|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是作业S1E2里的代码:
- #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[MAX], target[MAX];
-
- 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[MAX];
-
- 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[MAX] = ".";
-
- printf("计算中...\n");
-
- findAllCodes(path);
- findALLDirs(path);
-
- printf("目前你总共写了 %ld 行代码!\n\n", total);
- system("pause");
-
- return 0;
- }
复制代码
在vs2022不能编译成功的原因可能是由于您使用了不兼容的库和函数。
首先,您在代码中包含了多个头文件,但是没有使用这些头文件中的函数和变量。所以可以去掉无用的头文件包含。
其次,您定义了一些字符串数组,但是没有给它们分配足够的空间。应该将字符串数组的定义改成足够大的字符数组(例如`char thePath[MAX]`和`char target[MAX]`)。
然后,您使用了非标准的函数`_findfirst`、`_findnext`和`_findclose`来遍历文件目录和文件。在新版本的Visual Studio中,可以使用标准的C函数`opendir`、`readdir`和`closedir`来代替。这些函数可以通过包含头文件`<dirent.h>`来使用。下面是使用标准库函数的修改代码:
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <dirent.h>
- #define MAX 256
- long total;
- int countLines(const char *filename);
- void findAllCodes(const char *path);
- 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;
- }
- void findAllCodes(const char *path)
- {
- DIR *dir;
- struct dirent *entry;
- if ((dir = opendir(path)) == NULL)
- {
- fprintf(stderr, "Can not open the directory:%s\n", path);
- return;
- }
- while ((entry = readdir(dir)) != NULL)
- {
- if (entry->d_type == DT_REG && strstr(entry->d_name, ".c") != NULL)
- {
- char target[MAX];
- sprintf(target, "%s/%s", path, entry->d_name);
- total += countLines(target);
- }
- else if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
- {
- char subPath[MAX];
- sprintf(subPath, "%s/%s", path, entry->d_name);
- findAllCodes(subPath);
- }
- }
- closedir(dir);
- }
- void findAllDirs(const char *path)
- {
- DIR *dir;
- struct dirent *entry;
- if ((dir = opendir(path)) == NULL)
- {
- fprintf(stderr, "Can not open the directory:%s\n", path);
- return;
- }
- while ((entry = readdir(dir)) != NULL)
- {
- if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
- {
- char subPath[MAX];
- sprintf(subPath, "%s/%s", path, entry->d_name);
- findAllCodes(subPath);
- findAllDirs(subPath);
- }
- }
- closedir(dir);
- }
- int main()
- {
- char path[MAX] = ".";
- printf("计算中...\n");
- findAllCodes(path);
- findAllDirs(path);
- printf("目前你总共写了 %ld 行代码!\n\n", total);
- system("pause");
- return 0;
- }
复制代码
希望这个修改后的代码可以在vs2022中成功编译运行并得到正确的结果。记得包含`<dirent.h>`头文件。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|