鱼C论坛

 找回密码
 立即注册
查看: 1309|回复: 6

[已解决]strcpy(thePath, path); 为什么这一行一直显示有问题

[复制链接]
发表于 2020-10-1 16:05:43 | 显示全部楼层 |阅读模式

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

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

x

  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
最佳答案
2020-10-1 16:50:28
  1. // 该程序必须在 C 编译器下运行,C++ 编译器下不能运行!

  2. // 有些编译器对缩进也有严格的要求,所以,请不要改变每一行前的空格!

  3. #include <io.h>
  4. #include <direct.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>

  8. #define MAX        256

  9. long total;

  10. int countLines(const char *filename);
  11. void findAllCodes(const char *path);
  12. void findALLFiles(const char *path);

  13. int countLines(const char *filename)
  14. {
  15.     FILE *fp;
  16.     int count = 0;
  17.     int temp;

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

  23.     while ((temp = fgetc(fp))!= EOF)
  24.         {
  25.         if(temp == '\n')
  26.                 {
  27.             count++;
  28.                 }
  29.         }

  30.     fclose(fp);

  31.     return count;
  32. }

  33. void findAllCodes(const char *path)
  34. {
  35.     struct _finddata_t fa;
  36.     long handle;
  37.     char thePath[MAX], target[MAX];

  38.     strcpy(thePath,path);
  39.     if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  40.         {
  41.         do
  42.                 {
  43.             sprintf(target, "%s/%s", path, fa.name);
  44.             total += countLines(target);
  45.                 }
  46.         while (_findnext(handle, &fa) == 0);
  47.         }

  48.     _findclose(handle);
  49. }

  50. void findALLDirs(const char *path)
  51. {
  52.     struct _finddata_t fa;
  53.     long handle;
  54.     char thePath[MAX];

  55.     strcpy(thePath, path);
  56.     if((handle =  _findfirst(strcat(thePath, "/*"),&fa)) == -1L)
  57.         {
  58.         fprintf(stderr, "The path %s is wrong!\n",path);
  59.         return;
  60.         }

  61.     do
  62.         {
  63.         if(!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  64.             continue;

  65.         if( fa.attrib == _A_SUBDIR)
  66.                 {
  67.             sprintf(thePath, "%s/%s", path, fa.name);
  68.             findAllCodes(thePath);
  69.             findALLDirs(thePath);
  70.                 }
  71.         }
  72.     while (_findnext(handle, &fa) == 0);

  73.     _findclose(handle);
  74. }

  75. int main()
  76. {
  77.     char path[MAX] = ".";

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

  79.     findAllCodes(path);    // C要大写
  80.     findALLDirs(path);

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

  83.     return 0;
  84. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-10-1 16:43:00 | 显示全部楼层

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

#define MAX   256

long total;

int countLines(const char *filrname);
void findAllCodes(const char *path);
void findALLsFiles(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)
                          {
                                          print(stderr,"The path %s is wrong!\n",path);
                                          return;
                           }
                          
                           do
                           {
                                            if(!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("16:19目前你总共写了%1d 行代码\n\n",total);
                    system("pause");
                    
                    return 0;
    }
       

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

#define MAX   256

long total;

int countLines(const char *filrname);
void findAllCodes(const char *path);
void findALLsFiles(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)
                          {
                                          print(stderr,"The path %s is wrong!\n",path);
                                          return;
                           }
                          
                           do
                           {
                                            if(!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("16:19目前你总共写了%1d 行代码\n\n",total);
                    system("pause");
                    
                    return 0;
    }
       

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

#define MAX   256

long total;

int countLines(const char *filrname);
void findAllCodes(const char *path);
void findALLsFiles(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)
                          {
                                          print(stderr,"The path %s is wrong!\n",path);
                                          return;
                           }
                          
                           do
                           {
                                            if(!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("16:19目前你总共写了%1d 行代码\n\n",total);
                    system("pause");
                    
                    return 0;
    }
       

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

#define MAX   256

long total;

int countLines(const char *filrname);
void findAllCodes(const char *path);
void findALLsFiles(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)
                          {
                                          print(stderr,"The path %s is wrong!\n",path);
                                          return;
                           }
                          
                           do
                           {
                                            if(!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("16:19目前你总共写了%1d 行代码\n\n",total);
                    system("pause");
                    
                    return 0;
    }
       

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

#define MAX   256

long total;

int countLines(const char *filrname);
void findAllCodes(const char *path);
void findALLsFiles(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)
                          {
                                          print(stderr,"The path %s is wrong!\n",path);
                                          return;
                           }
                          
                           do
                           {
                                            if(!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("16:19目前你总共写了%1d 行代码\n\n",total);
                    system("pause");
                    
                    return 0;
    }
       

  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-1 16:44:08 | 显示全部楼层

  编译单个文件...
--------
- 文件名: C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.cpp
- 编译器名: TDM-GCC 4.9.2 64-bit Release

处理 C++ 源文件...
--------
- C++ 编译器: C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\g++.exe
- 命令: g++.exe "C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.cpp" -o "C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.exe"  -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib" -static-libgcc
C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.cpp: In function 'void findAllCodes(const char*)':
C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.cpp:47:3: error: expected initializer before 'strcpy'
   strcpy(thePath, path);
   ^

C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.cpp:52:12: error: 'target' was not declared in this scope
    sprintf(target, "%s/%s", path, fa.name);
            ^
C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.cpp: In function 'void findAllDirs(const char*)':
C:\Users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节课\超长代码的一部分.cpp:69:58: error: 'print' was not declared in this scope
               print(stderr,"The path %s is wrong!\n",path);  

这三处错误是什么意思??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-1 16:49:39 | 显示全部楼层
努力学C的我 发表于 2020-10-1 16:44
编译单个文件...
--------
- 文件名: C:%users\Administrator.WIN-1HG23H1KLBU\Desktop\c语言\第二节 ...

你这发的是什么呀?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-1 16:50:28 | 显示全部楼层    本楼为最佳答案   
  1. // 该程序必须在 C 编译器下运行,C++ 编译器下不能运行!

  2. // 有些编译器对缩进也有严格的要求,所以,请不要改变每一行前的空格!

  3. #include <io.h>
  4. #include <direct.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>

  8. #define MAX        256

  9. long total;

  10. int countLines(const char *filename);
  11. void findAllCodes(const char *path);
  12. void findALLFiles(const char *path);

  13. int countLines(const char *filename)
  14. {
  15.     FILE *fp;
  16.     int count = 0;
  17.     int temp;

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

  23.     while ((temp = fgetc(fp))!= EOF)
  24.         {
  25.         if(temp == '\n')
  26.                 {
  27.             count++;
  28.                 }
  29.         }

  30.     fclose(fp);

  31.     return count;
  32. }

  33. void findAllCodes(const char *path)
  34. {
  35.     struct _finddata_t fa;
  36.     long handle;
  37.     char thePath[MAX], target[MAX];

  38.     strcpy(thePath,path);
  39.     if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  40.         {
  41.         do
  42.                 {
  43.             sprintf(target, "%s/%s", path, fa.name);
  44.             total += countLines(target);
  45.                 }
  46.         while (_findnext(handle, &fa) == 0);
  47.         }

  48.     _findclose(handle);
  49. }

  50. void findALLDirs(const char *path)
  51. {
  52.     struct _finddata_t fa;
  53.     long handle;
  54.     char thePath[MAX];

  55.     strcpy(thePath, path);
  56.     if((handle =  _findfirst(strcat(thePath, "/*"),&fa)) == -1L)
  57.         {
  58.         fprintf(stderr, "The path %s is wrong!\n",path);
  59.         return;
  60.         }

  61.     do
  62.         {
  63.         if(!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  64.             continue;

  65.         if( fa.attrib == _A_SUBDIR)
  66.                 {
  67.             sprintf(thePath, "%s/%s", path, fa.name);
  68.             findAllCodes(thePath);
  69.             findALLDirs(thePath);
  70.                 }
  71.         }
  72.     while (_findnext(handle, &fa) == 0);

  73.     _findclose(handle);
  74. }

  75. int main()
  76. {
  77.     char path[MAX] = ".";

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

  79.     findAllCodes(path);    // C要大写
  80.     findALLDirs(path);

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

  83.     return 0;
  84. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-1 17:45:05 | 显示全部楼层

  1. #include<io.h>
  2. #include<direct.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>

  6. #define MAX   256

  7. long total;

  8. int countLines(const char* filrname);
  9. void findAllCodes(const char* path);
  10. void findALLsFiles(const char* path);

  11. int countLines(const char* filename)
  12. {
  13.     FILE* fp;
  14.     int count = 0;
  15.     int temp;

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

  21.     while ((temp = fgetc(fp)) != EOF)
  22.     {
  23.         if (temp == '\n')
  24.         {
  25.             count++;
  26.         }
  27.     }

  28.     fclose(fp);

  29.     return count;
  30. }

  31. void findAllCodes(const char* path)
  32. {
  33.     struct _finddata_t fa;
  34.     long handle;
  35.     char thePath[MAX], target[MAX];// 少了分号

  36.     strcpy(thePath, path);
  37.     if ((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  38.     {
  39.         do
  40.         {
  41.             sprintf(target, "%s/%s", path, fa.name);
  42.             total += countLines(target);
  43.         } while (_findnext(handle, &fa) == 0);
  44.     }

  45.     _findclose(handle);
  46. }

  47. void findAllDirs(const char* path)
  48. {
  49.     struct _finddata_t fa;
  50.     long handle;
  51.     char thePath[MAX];

  52.     strcpy(thePath, path);
  53.     if ((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  54.     {
  55.         // fprintf不是print
  56.         fprintf(stderr, "The path %s is wrong!\n", path);
  57.         return;
  58.     }

  59.     do
  60.     {
  61.         // 这一处抄错了
  62.         if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  63.             continue;

  64.         if (fa.attrib == _A_SUBDIR)
  65.         {
  66.             sprintf(thePath, "%s/%s", path, fa.name);
  67.             findAllCodes(thePath);
  68.             findAllDirs(thePath);
  69.         }
  70.     } while (_findnext(handle, &fa) == 0);

  71.     _findclose(handle);
  72. }

  73. int main()
  74. {
  75.     char path[MAX] = ".";

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

  77.     findAllCodes(path);
  78.     findAllDirs(path);
  79.     // 小写l 不是1
  80.     printf("16:19目前你总共写了%ld行代码\n\n", total);
  81.     system("pause");

  82.     return 0;
  83. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-1 17:46:42 | 显示全部楼层
45行少了分号,70行是fprintf不是print, 77行抄错了
100是ld(小写L)不是1d(不是数字1)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-23 15:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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