|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- main ()
- {
- FILE *fp1,*fp2;
- char str[128];
- if((fp1=fopen("D:\\test1.txt","r"))==NULL)
- {
- printf("can not open file\n");
- exit (0);
- }
- if((fp2=fopen("D:\\test2.txt","w+"))==NULL)
- {
- printf("can not open file\n");
- exit (0);
- };
- while ((strlen(fgets(str,128,fp1)))>0)
- {
- fputs(str,fp2);
- printf("%s",str);
- }
- fclose(fp1);
- fclose(fp2);
- }
复制代码
这样的一段代码,写入D盘中,text1是有内容的,运行后只创建text2,却写不进去,显示屏也是有内容的,是我电脑问题吗?你们试试哈
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXSIZE 65535
- int main()
- {
- FILE *fp1, *fp2;
- char str[1000];
- if((fp1 = fopen("1.txt", "r")) == NULL)
- {
- printf("Opening file error!\n");
- exit(EXIT_FAILURE);
- }
- if((fp2 = fopen("2.txt", "w")) == NULL)
- {
- printf("Opening file error!\n");
- exit(EXIT_FAILURE);
- }
- while(!feof(fp1))
- {
- if(fgets(str, MAXSIZE, fp1) != NULL)
- {
- fputs(str, fp2);
- printf("%s", str);
- }
- }
- fclose(fp1);
- fclose(fp2);
- return 0;
- }
复制代码我的可以运行,文件也创建成功了。你的程序的问题在于strlen()函数里的参数不可以传入空指针,编译正确但是运行会产生错误。
这个是直接用你的改的,并没有报错,文件创建成功,且将1.txt成功复制到3.txt了。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main ()
- {
- FILE *fp1,*fp2;
- char str[128];
- if((fp1=fopen("1.txt","r"))==NULL)
- {
- printf("can not open file\n");
- exit(0);
- }
- if((fp2=fopen("3.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);
- }
复制代码
|
|