晴栀竹音 发表于 2020-8-9 11:21:32

新手求助小甲鱼s1e2的作业问题

我用的是vs2019的,然后将s1e2的作业代码打入后vs2019提醒我要在某些函数下面加上   _s
但是在加入以后还是提示说 函数中调用的参数太少该怎么解决啊

晴栀竹音 发表于 2020-8-9 11:22:42

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#define MAX 256

long total;

int countLines(const char *filename);
int isCode(const char *filename);
void findAllDirs(const char *path);

int countLines(const char *filename)
{
      FILE *fp;
      int count = 0;
      int temp;

      if ((fp = fopen(filename, "r")) == NULL)
      {
                fprintf(stderr, "Can not open the file: %s\n", filename);
                return 0;
      }

      while ((temp = fgetc(fp)) != EOF)
      {
                if (temp == '\n')
                {
                        count++;
                }
      }

      fclose(fp);

      return count;
}

int isCode(const char *filename)
{
      int length;

      length = strlen(filename);
      
      if (!strcmp(filename + (length - 2), ".c"))
      {
                return 1;
      }
      else
      {
                return 0;
      }
}

void findAllDirs(const char *path)
{
      DIR *dp;
      struct dirent *entry;
      struct stat statbuf;

      if ((dp = opendir(path)) == NULL)
      {
                fprintf(stderr, "The path %s is wrong!\n", path);
                return;
      }

      chdir(path);
      while ((entry = readdir(dp)) != NULL)
      {
                lstat(entry->d_name, &statbuf);

                if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
                        continue;

                if (S_ISDIR(statbuf.st_mode))
                {
                        findAllDirs(entry->d_name);
                }
                else
                {
                        if (isCode(entry->d_name))
                        {
                              total += countLines(entry->d_name);
                        }
                }
      }

      chdir("..");
      closedir(dp);
}

int main()
{
      char path = ".";

      printf("计算中...\n");

      findAllDirs(path);

      printf("目前你总共写了 %ld 行代码!\n\n", total);

      return 0;
}

晴栀竹音 发表于 2020-8-9 11:23:18

求解{:10_266:}

zltzlt 发表于 2020-8-9 11:24:07

晴栀竹音 发表于 2020-8-9 11:22
#include
#include
#include


你用的是什么系统?

晴栀竹音 发表于 2020-8-9 11:28:13

zltzlt 发表于 2020-8-9 11:24
你用的是什么系统?

用的是win10 没安装Linus

zltzlt 发表于 2020-8-9 11:29:32

晴栀竹音 发表于 2020-8-9 11:28
用的是win10 没安装Linus

你抄错了吧,Windows 系统应该是下面的代码:


#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX      256

long total;

int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(const char *path);

int countLines(const char *filename)
{
      FILE *fp;
      int count = 0;
      int temp;
      
      if ((fp = fopen(filename, "r")) == NULL)
      {
                fprintf(stderr, "Can not open the file:%s\n", filename);
                return 0;
      }
      
      while ((temp = fgetc(fp)) != EOF)
      {
                if (temp == '\n')
                {
                        count++;
                }
      }
      
      fclose(fp);
      
      return count;
}

void findAllCodes(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath, target;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
      {
                do
                {
                        sprintf(target, "%s/%s", path, fa.name);
                        total += countLines(target);
                }while (_findnext(handle, &fa) == 0);
      }
   
      _findclose(handle);
}

void findALLDirs(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
      {
                fprintf(stderr, "The path %s is wrong!\n",path);
                return;
      }
   
      do
      {      
                if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
                        continue;
                  
                if( fa.attrib == _A_SUBDIR)
                {      
                        sprintf(thePath, "%s/%s", path, fa.name);
                        findAllCodes(thePath);
                        findALLDirs(thePath);
                }
      }while (_findnext(handle, &fa) == 0);
   
      _findclose(handle);   
}

int main()
{
      char path = ".";
      
      printf("计算中...\n");
      
      findAllCodes(path);
      findALLDirs(path);
      
      printf("目前你总共写了 %ld 行代码!\n\n", total);
      system("pause");
      
      return 0;
}

晴栀竹音 发表于 2020-8-9 11:34:48

zltzlt 发表于 2020-8-9 11:29
你抄错了吧,Windows 系统应该是下面的代码:

不行啊,换成了这些代码也还是一样的问题,要我在那些函数下面加上_s
然后加完了又提升我说函数中调用的参数太少

zltzlt 发表于 2020-8-9 11:40:08

晴栀竹音 发表于 2020-8-9 11:34
不行啊,换成了这些代码也还是一样的问题,要我在那些函数下面加上_s
然后加完了又提升我说函数中调用 ...

方便截个图嘛?

图床地址:https://imgchr.com/

晴栀竹音 发表于 2020-8-9 11:43:55

zltzlt 发表于 2020-8-9 11:40
方便截个图嘛?

图床地址:https://imgchr.com/

https://imgchr.com/i/aob8Gd

zltzlt 发表于 2020-8-9 11:47:25

晴栀竹音 发表于 2020-8-9 11:43


这个换 Dev-C++ 应该可以正常运行

晴栀竹音 发表于 2020-8-9 11:48:25

zltzlt 发表于 2020-8-9 11:47
这个换 Dev-C++ 应该可以正常运行

是vs2019新版本的问题吗?

zltzlt 发表于 2020-8-9 11:48:56

晴栀竹音 发表于 2020-8-9 11:48
是vs2019新版本的问题吗?

也不是 VS 的问题,是编译器不同

晴栀竹音 发表于 2020-8-9 11:50:33

zltzlt 发表于 2020-8-9 11:48
也不是 VS 的问题,是编译器不同

好的,我换一个Dev-c++试一试,谢谢啦{:5_109:}

永恒的蓝色梦想 发表于 2020-8-9 11:58:52

晴栀竹音 发表于 2020-8-9 11:50
好的,我换一个Dev-c++试一试,谢谢啦

不需要换 DEV C++,只需要在代码开头加#define _CRT_SECURE_NO_WARNINGS就可以正常运行代码了

永恒的蓝色梦想 发表于 2020-8-9 11:59:09

zltzlt 发表于 2020-8-9 11:48
也不是 VS 的问题,是编译器不同

DEV 与 VS 之争{:10_277:}

晴栀竹音 发表于 2020-8-9 13:26:17

永恒的蓝色梦想 发表于 2020-8-9 11:58
不需要换 DEV C++,只需要在代码开头加就可以正常运行代码了

果然可以,成了,

晴栀竹音 发表于 2020-8-9 13:27:34

永恒的蓝色梦想 发表于 2020-8-9 11:58
不需要换 DEV C++,只需要在代码开头加就可以正常运行代码了

以后类似的问题也可以用这串代码解决吗?

不过加完之后虽然成功调试了,但是却没显示出来我写了几行代码{:10_250:}

永恒的蓝色梦想 发表于 2020-8-9 13:33:34

晴栀竹音 发表于 2020-8-9 13:27
以后类似的问题也可以用这串代码解决吗?

不过加完之后虽然成功调试了,但是却没显示出来我写了几行代 ...

如果你仔细看报错的话,上面写着定义 _CRT_SECURE_NO_WARNINGS 就可以取消错误的{:10_277:}

晴栀竹音 发表于 2020-8-9 13:37:23

永恒的蓝色梦想 发表于 2020-8-9 13:33
如果你仔细看报错的话,上面写着定义 _CRT_SECURE_NO_WARNINGS 就可以取消错误的

被大佬提醒后我看见了{:10_254:}

永恒的蓝色梦想 发表于 2020-8-9 13:38:55

晴栀竹音 发表于 2020-8-9 13:27
以后类似的问题也可以用这串代码解决吗?

不过加完之后虽然成功调试了,但是却没显示出来我写了几行代 ...

代码的问题,你等一下,我先看看
页: [1] 2
查看完整版本: 新手求助小甲鱼s1e2的作业问题