鱼C论坛

 找回密码
 立即注册
查看: 1892|回复: 12

[已解决]刚接触C看了 [课后作业] S1E2:第一个程序 【完全复制,也无法运行】

[复制链接]
发表于 2018-10-18 19:54:03 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 益脑三通 于 2018-10-18 19:59 编辑

本人是0基础啊,勿喷,根据小鱼要求的 第一课 抄一遍 第一个程序【计算代码量的程序】

抄完了 运行报错:

1>main.c
1>c:\users\ipiqi\source\repos\c_course1\c_course1\main.c(22): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>f:\windows kits\10\include\10.0.17134.0\ucrt\stdio.h(208): note: 参见“fopen”的声明
1>c:\users\ipiqi\source\repos\c_course1\c_course1\main.c(46): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>f:\windows kits\10\include\10.0.17134.0\ucrt\string.h(133): note: 参见“strcpy”的声明
1>c:\users\ipiqi\source\repos\c_course1\c_course1\main.c(47): error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>f:\windows kits\10\include\10.0.17134.0\ucrt\string.h(90): note: 参见“strcat”的声明
1>c:\users\ipiqi\source\repos\c_course1\c_course1\main.c(50): error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>f:\windows kits\10\include\10.0.17134.0\ucrt\stdio.h(1774): note: 参见“sprintf”的声明
1>c:\users\ipiqi\source\repos\c_course1\c_course1\main.c(63): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>f:\windows kits\10\include\10.0.17134.0\ucrt\string.h(133): note: 参见“strcpy”的声明
1>c:\users\ipiqi\source\repos\c_course1\c_course1\main.c(64): error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>f:\windows kits\10\include\10.0.17134.0\ucrt\string.h(90): note: 参见“strcat”的声明
1>c:\users\ipiqi\source\repos\c_course1\c_course1\main.c(76): error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>f:\windows kits\10\include\10.0.17134.0\ucrt\stdio.h(1774): note: 参见“sprintf”的声明
1>已完成生成项目“C_Course1.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========



一开始以为是自己抄错了 特意对了一遍 发现 没问题 !
然后复制了一份网页原文去VS2017上面 运行也发生了同意的报错!


求解啊 或者有没有 遇到同样问题的 同学们呢!


本人是 Windows10系统,开发环境:VS2017


我的代码如下:

#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 findAllFfiles(const char *path);


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

        if ((fp = fopen(filename, "r")) == NULL)
        {
                fprintf(stderr, "Can not open the file:%s <=> 无法打开这个文件:%s\n", filename, 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! <=> 这个路径 %s 是错误的!\n", path, 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;
}
最佳答案
2018-10-18 20:32:21
  1. #include<stdio.h>
  2. #include<direct.h>
  3. #include<io.h>
  4. #include<stdlib.h>
  5. #include<string.h>

  6. #define MAX 256

  7. long total;

  8. int countLines(const char *filename);
  9. void findAllCodes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count = 0;
  15.         int temp;
  16.         
  17.         if((fp = fopen(filename,"r")) == NULL)
  18.         {
  19.                 fprintf(stderr,"Can not open the file:%s\n", filename);
  20.                 return 0;
  21.         }
  22.         while((temp = fgetc(fp)) != EOF)
  23.         {
  24.             if(temp == '\n')
  25.                 {
  26.                         count++;
  27.                 }
  28.         }
  29.         fclose(fp);
  30.         return count;
  31. }

  32. void findAllCodes(const char *path)
  33. {
  34.         struct _finddata_t fa;
  35.         long handle;
  36.         char thePath[MAX], target[MAX];
  37.         
  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.                 }while(_findnext(handle,&fa) == 0);
  46.         }
  47.         _findclose(handle);
  48. }
  49. void findALLDirs(const char *path)
  50. {
  51.         struct _finddata_t fa;
  52.         long handle;
  53.         char thePath[MAX];
  54.         strcpy(thePath,path);
  55.         if((handle = _findfirst(strcat(thePath,"/*"), &fa)) == -1L)
  56.         {
  57.                 fprintf(stderr, "The path %s is wrong!\n", path);
  58.                 return;
  59.         }
  60.         do
  61.         {
  62.                 if( !strcmp(fa.name,".") || !strcmp(fa.name,"..") )
  63.                 continue;
  64.                
  65.                 if(fa.attrib == _A_SUBDIR)
  66.                 {
  67.                         sprintf(thePath,"%s/%s", path, fa.name);
  68.                         findAllCodes(thePath);
  69.                         findALLDirs(thePath);
  70.                 }
  71.         }while(_findnext(handle, &fa) == 0);
  72.         
  73.         _findclose(handle);
  74.         
  75. }

  76. int main()
  77. {
  78.         char path[MAX]=".";
  79.         printf("计算中...\n");
  80.         
  81.         findAllCodes(path);
  82.         
  83.         findALLDirs(path);
  84.         
  85.         printf("目前你总共写了 %ld 行代码!\n\b", total);
  86.         system("pause");
  87.         return 0;
  88. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-18 20:20:23 | 显示全部楼层
你建立的项目 --> Windows 桌面 >> Windows 桌面导向 >> 控制台应用程序
空项目(打勾),安全……(取消)

你这一句对??
  1. void findALLDirs(const char *path)
  2. {
  3.         ……
  4.         strcpy(thePath, path);
  5.         if ((handle = _findfirst(strcht(thePath, "/*"), &fa)) == -1L)  //strcht
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-18 20:32:21 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<direct.h>
  3. #include<io.h>
  4. #include<stdlib.h>
  5. #include<string.h>

  6. #define MAX 256

  7. long total;

  8. int countLines(const char *filename);
  9. void findAllCodes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count = 0;
  15.         int temp;
  16.         
  17.         if((fp = fopen(filename,"r")) == NULL)
  18.         {
  19.                 fprintf(stderr,"Can not open the file:%s\n", filename);
  20.                 return 0;
  21.         }
  22.         while((temp = fgetc(fp)) != EOF)
  23.         {
  24.             if(temp == '\n')
  25.                 {
  26.                         count++;
  27.                 }
  28.         }
  29.         fclose(fp);
  30.         return count;
  31. }

  32. void findAllCodes(const char *path)
  33. {
  34.         struct _finddata_t fa;
  35.         long handle;
  36.         char thePath[MAX], target[MAX];
  37.         
  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.                 }while(_findnext(handle,&fa) == 0);
  46.         }
  47.         _findclose(handle);
  48. }
  49. void findALLDirs(const char *path)
  50. {
  51.         struct _finddata_t fa;
  52.         long handle;
  53.         char thePath[MAX];
  54.         strcpy(thePath,path);
  55.         if((handle = _findfirst(strcat(thePath,"/*"), &fa)) == -1L)
  56.         {
  57.                 fprintf(stderr, "The path %s is wrong!\n", path);
  58.                 return;
  59.         }
  60.         do
  61.         {
  62.                 if( !strcmp(fa.name,".") || !strcmp(fa.name,"..") )
  63.                 continue;
  64.                
  65.                 if(fa.attrib == _A_SUBDIR)
  66.                 {
  67.                         sprintf(thePath,"%s/%s", path, fa.name);
  68.                         findAllCodes(thePath);
  69.                         findALLDirs(thePath);
  70.                 }
  71.         }while(_findnext(handle, &fa) == 0);
  72.         
  73.         _findclose(handle);
  74.         
  75. }

  76. int main()
  77. {
  78.         char path[MAX]=".";
  79.         printf("计算中...\n");
  80.         
  81.         findAllCodes(path);
  82.         
  83.         findALLDirs(path);
  84.         
  85.         printf("目前你总共写了 %ld 行代码!\n\b", total);
  86.         system("pause");
  87.         return 0;
  88. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-18 22:13:19 | 显示全部楼层

感谢 这个复制了就可以了 我会重新再抄一遍的
虽然这个问题很简单 但是 我真是 一个字一个字 抄过来的 应该是抄错某字了吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-18 22:17:54 | 显示全部楼层
益脑三通 发表于 2018-10-18 22:13
感谢 这个复制了就可以了 我会重新再抄一遍的
虽然这个问题很简单 但是 我真是 一个字一个字 抄过来的  ...

一字一句的抄也是一种学习
嗯我给的, 60 行,strcat
不过你也是换项目属性了吧?不然是报错累累的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 09:17:31 From FishC Mobile | 显示全部楼层
claws0n 发表于 2018-10-18 22:17
一字一句的抄也是一种学习
嗯我给的, 60 行,strcat
不过你也是换项目属性了吧?不然是报错 ...

对的应该是安全检查
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 09:26:45 | 显示全部楼层
益脑三通 发表于 2018-10-19 09:17
对的应该是安全检查

嗯,从报错变成警告而已,忽略就好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-20 19:00:06 | 显示全部楼层
安全检查导致的,加一句宏就可以了

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

使用道具 举报

 楼主| 发表于 2018-10-23 21:48:26 | 显示全部楼层
回访三生 发表于 2018-10-20 19:00
安全检查导致的,加一句宏就可以了

#define _CRT_SECURE_NO_WARNINGS

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

使用道具 举报

发表于 2019-11-8 10:40:59 | 显示全部楼层
为什么我怎么搞都是0行啊
小甲鱼的是0行  
用你们的也是0行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-8 21:04:27 | 显示全部楼层
凉月流沐 发表于 2019-11-8 10:40
为什么我怎么搞都是0行啊
小甲鱼的是0行  
用你们的也是0行

我还以为就我以个人是这样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-15 12:18:13 | 显示全部楼层
我运行也是零行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-20 22:45:50 | 显示全部楼层
我windows的运行109行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 21:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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