鱼C论坛

 找回密码
 立即注册
查看: 1973|回复: 3

[已解决]求大佬们帮帮忙,运行没有错误。为啥显示的CAN NOT OPEN 那些文件,是程序问题还是...

[复制链接]
发表于 2022-3-13 10:52:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <io.h>
#include <direct.h>
#include<stdio.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;
}
最佳答案
2022-3-13 12:56:33
路径问题,你在两个 %s 之间加个斜杆就可以了:

  1. #include <io.h>
  2. #include <direct.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #define MAX 256

  6. long total;

  7. int countLines(const char *filename);
  8. void findALLCodes(const char *path);
  9. void findALLFiles(const char *path);

  10. int countLines(const char *filename)
  11. {
  12.         FILE *fp;
  13.         int count=0;
  14.         int temp;
  15.         if((fp=fopen(filename,"r"))==NULL)
  16.     {
  17.             fprintf(stderr,"Can not open the file:%s\n",filename);
  18.             return 0;
  19.         }      
  20.         while((temp = fgetc(fp)) != EOF )
  21.         {
  22.                 if(temp == '\n')
  23.                 {
  24.                         count++;
  25.                 }
  26.                
  27.         }
  28.       
  29.         fclose(fp);
  30.       
  31.         return count;
  32.                
  33. }
  34. void findALLCodes(const char *path)
  35. {
  36.         struct _finddata_t fa;
  37.          long handle;
  38.          char thePath[MAX],target[MAX];
  39.          
  40.          strcpy(thePath,path);
  41.          if((handle=_findfirst(strcat(thePath,"/*.c"),&fa))!=-1L)
  42.          {
  43.                  do
  44.                  {
  45.                          sprintf(target,"%s/%s",path,fa.name);                  // 改了这行
  46.                          total += countLines(target);
  47.                  }
  48.                  while (_findnext(handle,&fa) == 0);
  49.                  
  50.           }
  51.           _findclose(handle);      
  52.                
  53. }

  54. void findALLDirs(const char *path)
  55. {
  56.         struct _finddata_t fa;
  57.         long handle;
  58.         char thePath[MAX];
  59.       
  60.         strcpy(thePath,path);
  61.         if((handle=_findfirst(strcat(thePath,"/*"),&fa))==-1L)
  62.         {
  63.                 fprintf(stderr,"The path %s is wrong!\n",path);
  64.                 return;
  65.          }
  66.         do
  67.         {
  68.                
  69.                 if (!strcmp(fa.name,".")||!strcmp(fa.name,".."))
  70.                 continue;
  71.                
  72.                
  73.                 if(fa.attrib==_A_SUBDIR)
  74.                 {
  75.                         sprintf(thePath,"%s/%s",path,fa.name);                // 改了这行
  76.                         findALLCodes(thePath);
  77.                         findALLDirs(thePath);
  78.                        
  79.                  }
  80.          } while (_findnext(handle,&fa)==0);
  81.          
  82.          _findclose(handle);
  83.       
  84. }

  85. int main ()
  86. {
  87.         char path[MAX] =".";
  88.       
  89.         printf("计算中...\n");
  90.       
  91.         findALLCodes(path);
  92.         findALLDirs(path);
  93.       
  94.         printf("目前你总共写了%ld 行代码!\n\n",total);
  95.         system("pause");
  96.       
  97.         return 0;
  98. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-13 10:54:23 | 显示全部楼层
运行没有报错,就是无法统计当前文件夹内程序的行数,显示的是无法打开那个文件,希望有路过的大佬帮忙看一哈,感激不尽,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-13 12:56:33 | 显示全部楼层    本楼为最佳答案   
路径问题,你在两个 %s 之间加个斜杆就可以了:

  1. #include <io.h>
  2. #include <direct.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #define MAX 256

  6. long total;

  7. int countLines(const char *filename);
  8. void findALLCodes(const char *path);
  9. void findALLFiles(const char *path);

  10. int countLines(const char *filename)
  11. {
  12.         FILE *fp;
  13.         int count=0;
  14.         int temp;
  15.         if((fp=fopen(filename,"r"))==NULL)
  16.     {
  17.             fprintf(stderr,"Can not open the file:%s\n",filename);
  18.             return 0;
  19.         }      
  20.         while((temp = fgetc(fp)) != EOF )
  21.         {
  22.                 if(temp == '\n')
  23.                 {
  24.                         count++;
  25.                 }
  26.                
  27.         }
  28.       
  29.         fclose(fp);
  30.       
  31.         return count;
  32.                
  33. }
  34. void findALLCodes(const char *path)
  35. {
  36.         struct _finddata_t fa;
  37.          long handle;
  38.          char thePath[MAX],target[MAX];
  39.          
  40.          strcpy(thePath,path);
  41.          if((handle=_findfirst(strcat(thePath,"/*.c"),&fa))!=-1L)
  42.          {
  43.                  do
  44.                  {
  45.                          sprintf(target,"%s/%s",path,fa.name);                  // 改了这行
  46.                          total += countLines(target);
  47.                  }
  48.                  while (_findnext(handle,&fa) == 0);
  49.                  
  50.           }
  51.           _findclose(handle);      
  52.                
  53. }

  54. void findALLDirs(const char *path)
  55. {
  56.         struct _finddata_t fa;
  57.         long handle;
  58.         char thePath[MAX];
  59.       
  60.         strcpy(thePath,path);
  61.         if((handle=_findfirst(strcat(thePath,"/*"),&fa))==-1L)
  62.         {
  63.                 fprintf(stderr,"The path %s is wrong!\n",path);
  64.                 return;
  65.          }
  66.         do
  67.         {
  68.                
  69.                 if (!strcmp(fa.name,".")||!strcmp(fa.name,".."))
  70.                 continue;
  71.                
  72.                
  73.                 if(fa.attrib==_A_SUBDIR)
  74.                 {
  75.                         sprintf(thePath,"%s/%s",path,fa.name);                // 改了这行
  76.                         findALLCodes(thePath);
  77.                         findALLDirs(thePath);
  78.                        
  79.                  }
  80.          } while (_findnext(handle,&fa)==0);
  81.          
  82.          _findclose(handle);
  83.       
  84. }

  85. int main ()
  86. {
  87.         char path[MAX] =".";
  88.       
  89.         printf("计算中...\n");
  90.       
  91.         findALLCodes(path);
  92.         findALLDirs(path);
  93.       
  94.         printf("目前你总共写了%ld 行代码!\n\n",total);
  95.         system("pause");
  96.       
  97.         return 0;
  98. }
复制代码

评分

参与人数 1荣誉 +2 鱼币 +1 贡献 +2 收起 理由
炫笑555 + 2 + 1 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-3-13 14:40:42 | 显示全部楼层
isdkz 发表于 2022-3-13 12:56
路径问题,你在两个 %s 之间加个斜杆就可以了:

感谢大佬,是我抄作业不认真了,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-17 00:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表