|  | 
 
| 
代码如下
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  复制代码
#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;
}
系统是windows
 编辑器为Dev-C++时,结果为106
 编辑器为vscode时,结果为0
 
出现在不同编辑器中编译运行出不同结果的原因可能有以下几个可能性:
 1. 编译环境不同:不同的编辑器可能使用不同的编译器或编译选项,这可能导致在某些情况下代码的行为不一致。例如,某些编译器可能对未定义的行为进行不同的处理,导致程序的输出结果不同。
 
 2. 版本差异:不同的编辑器和编译器版本可能存在差异,这可能导致编译器对语言标准的支持程度不同,或者在实现某些特性时存在差异,从而导致代码在不同环境下的运行结果不同。
 
 3. 路径和文件处理差异:不同的操作系统和编辑器可能对路径和文件处理有不同的方式。这可能导致代码在不同环境下找不到文件或发生其他错误。
 
 对于您提供的代码来说,主要问题可能是路径处理的差异。代码中使用了 Windows 特定的函数 _findfirst 和 _findnext 来遍历文件和目录,在不同的环境下可能会导致不同的结果。
 
 为了解决这个问题,您可以考虑使用跨平台的库和函数,如 <dirent.h> 和 <sys/stat.h>,来获取文件和目录信息,并改写代码以适应不同的操作系统和编辑器。这样可以提高代码的可移植性和兼容性。
 
 另外,您在代码中使用了一些 Windows 特定的头文件 <io.h> 和 <direct.h>,这也可能导致在非 Windows 环境下出现问题。建议您使用标准的 C/C++ 头文件来替代它们,例如 <stdio.h> 和 <unistd.h>。
 
 总之,不同编辑器中编译运行出不同结果的问题是多方面的,需要仔细检查代码的跨平台性、编译环境和依赖库的差异等因素,并相应地进行修改和调试。
 如果问题已经解决,请设置最佳答案
 | 
 |