鱼C论坛

 找回密码
 立即注册
查看: 841|回复: 4

[已解决]文件问题

[复制链接]
发表于 2020-3-25 17:33:22 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. main ()
  5. {       
  6.         FILE *fp1,*fp2;
  7.         char str[128];
  8.         if((fp1=fopen("D:\\test1.txt","r"))==NULL)
  9.         {
  10.                 printf("can not open file\n");
  11.                 exit (0);
  12.         }
  13.         if((fp2=fopen("D:\\test2.txt","w+"))==NULL)
  14.         {
  15.                 printf("can not open file\n");
  16.                 exit (0);
  17.         };
  18.         while ((strlen(fgets(str,128,fp1)))>0)
  19.         {
  20.                 fputs(str,fp2);
  21.                 printf("%s",str);
  22.         }
  23.         fclose(fp1);
  24.         fclose(fp2);

  25. }
复制代码

这样的一段代码,写入D盘中,text1是有内容的,运行后只创建text2,却写不进去,显示屏也是有内容的,是我电脑问题吗?你们试试哈
最佳答案
2020-3-26 13:03:35
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXSIZE 65535

  4. int main()
  5. {
  6.     FILE *fp1, *fp2;
  7.     char str[1000];
  8.     if((fp1 = fopen("1.txt", "r")) == NULL)
  9.     {
  10.         printf("Opening file error!\n");
  11.         exit(EXIT_FAILURE);
  12.     }
  13.     if((fp2 = fopen("2.txt", "w")) == NULL)
  14.     {
  15.         printf("Opening file error!\n");
  16.         exit(EXIT_FAILURE);
  17.     }

  18.     while(!feof(fp1))
  19.     {
  20.         if(fgets(str, MAXSIZE, fp1) != NULL)
  21.         {
  22.             fputs(str, fp2);
  23.             printf("%s", str);
  24.         }
  25.     }

  26.     fclose(fp1);
  27.     fclose(fp2);

  28.     return 0;
  29. }
复制代码
我的可以运行,文件也创建成功了。你的程序的问题在于strlen()函数里的参数不可以传入空指针,编译正确但是运行会产生错误。

这个是直接用你的改的,并没有报错,文件创建成功,且将1.txt成功复制到3.txt了。

  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #include <string.h>
  4.     int main ()
  5.     {
  6.             FILE *fp1,*fp2;
  7.             char str[128];
  8.             if((fp1=fopen("1.txt","r"))==NULL)
  9.             {
  10.                     printf("can not open file\n");
  11.                     exit(0);
  12.             }
  13.             if((fp2=fopen("3.txt","w+"))==NULL)
  14.             {
  15.                     printf("can not open file\n");
  16.                     exit(0);
  17.             };
  18.             while (fgets(str,128,fp1) != NULL)
  19.             {
  20.                     fputs(str,fp2);
  21.                     printf("%s",str);
  22.             }
  23.             fclose(fp1);
  24.             fclose(fp2);

  25.     }
复制代码




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

使用道具 举报

发表于 2020-3-25 23:13:19 | 显示全部楼层
循环改成这样while ((fgets(str, 128, fp1)!=NULL))试试

fgets函数读不到会返回NULL,你strlen(NULL)>0啥意思啊,改过来吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-26 11:11:36 | 显示全部楼层
入门者 发表于 2020-3-25 23:13
循环改成这样while ((fgets(str, 128, fp1)!=NULL))试试

fgets函数读不到会返回NULL,你strlen(NULL)>0啥 ...

出错,编译都过不了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-26 13:03:35 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXSIZE 65535

  4. int main()
  5. {
  6.     FILE *fp1, *fp2;
  7.     char str[1000];
  8.     if((fp1 = fopen("1.txt", "r")) == NULL)
  9.     {
  10.         printf("Opening file error!\n");
  11.         exit(EXIT_FAILURE);
  12.     }
  13.     if((fp2 = fopen("2.txt", "w")) == NULL)
  14.     {
  15.         printf("Opening file error!\n");
  16.         exit(EXIT_FAILURE);
  17.     }

  18.     while(!feof(fp1))
  19.     {
  20.         if(fgets(str, MAXSIZE, fp1) != NULL)
  21.         {
  22.             fputs(str, fp2);
  23.             printf("%s", str);
  24.         }
  25.     }

  26.     fclose(fp1);
  27.     fclose(fp2);

  28.     return 0;
  29. }
复制代码
我的可以运行,文件也创建成功了。你的程序的问题在于strlen()函数里的参数不可以传入空指针,编译正确但是运行会产生错误。

这个是直接用你的改的,并没有报错,文件创建成功,且将1.txt成功复制到3.txt了。

  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #include <string.h>
  4.     int main ()
  5.     {
  6.             FILE *fp1,*fp2;
  7.             char str[128];
  8.             if((fp1=fopen("1.txt","r"))==NULL)
  9.             {
  10.                     printf("can not open file\n");
  11.                     exit(0);
  12.             }
  13.             if((fp2=fopen("3.txt","w+"))==NULL)
  14.             {
  15.                     printf("can not open file\n");
  16.                     exit(0);
  17.             };
  18.             while (fgets(str,128,fp1) != NULL)
  19.             {
  20.                     fputs(str,fp2);
  21.                     printf("%s",str);
  22.             }
  23.             fclose(fp1);
  24.             fclose(fp2);

  25.     }
复制代码




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

使用道具 举报

发表于 2020-3-26 21:41:38 | 显示全部楼层
张世来4610484 发表于 2020-3-26 11:11
出错,编译都过不了

我在vs2013三上运行确实没问题

#include <stdio.h>
#include <stdlib.h>
int main()
{
        FILE *fp1, *fp2;
        char str[128];
        if ((fp1 = fopen("test1.txt", "r")) == NULL)
        {
                printf("can not open file\n");
                exit(0);
        }
        if ((fp2 = fopen("test2.txt", "w+")) == NULL)
        {
                printf("can not open file\n");
                exit(0);
        };
        while ((fgets(str, 128, fp1)!=NULL))
        {
                fputs(str, fp2);
                printf("%s", str);
        }
        fclose(fp1);
        fclose(fp2);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-2 19:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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