error:fgetc函数无法获取文本文档中的内容

来源:互联网 发布:数码宝贝2知乎 编辑:程序博客网 时间:2024/06/13 07:10

1、和以前的正确代码对了很久,都没发现是哪儿出问题了。

2、我开始把正确的代码的某些部分复制过来,结果还是没发现错误。

3、我开始尝试着,一行一行地注释掉某些代码,结果还是没有发现错误。当我即将厌烦即将绝望时,竟想到可能是文件指针出问题了,结果还真是,我定义了两个文件指针,将这两个文件指针都指向了一个变量,因此,只有后一个指针有效。

4、正确的做法,应该是删除最后一个指针。

错误代码如下:

#include <stdio.h>#include <IOSTREAM.H>#include<string.H>int main(){<span style="white-space:pre"></span>FILE *fRead,*fWrite;<span style="white-space:pre"></span>char chChar = 'a';<span style="white-space:pre"></span>char szFileContent[100];<span style="white-space:pre"></span>int i = 0;<span style="white-space:pre"></span>fRead = fopen("d:\\1.txt","r+");<span style="white-space:pre"></span>fWrite = fopen("d:\\1.txt","wt");<span style="white-space:pre"></span><span style="white-space:pre"></span>memset(szFileContent,0,sizeof(char)*100);<span style="white-space:pre"></span>if (fRead != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>fseek(fRead,0,SEEK_SET);<span style="white-space:pre"></span><span style="white-space:pre"></span> while((chChar = fgetc(fRead))&&(chChar != EOF))           {              putchar(chChar);  <span style="white-space:pre"></span>szFileContent[i] = chChar ;<span style="white-space:pre"></span>i++;        }  <span style="white-space:pre"></span>szFileContent[i] = 0;<span style="white-space:pre"></span>fclose(fRead);<span style="white-space:pre"></span>fRead = NULL;<span style="white-space:pre"></span><span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>cout<<"can't open the txt"<<endl;<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>if (fWrite != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>fprintf(fWrite," open the write-only txt");<span style="white-space:pre"></span>cout<<" open the write-only txt"<<endl;<span style="white-space:pre"></span>fclose(fWrite);<span style="white-space:pre"></span>fWrite = NULL;<span style="white-space:pre"></span>} <span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>cout<<"can't open the txt"<<endl;<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>cout<<endl<<"szFileContent:"<<szFileContent<<endl;<span style="white-space:pre"></span><span style="white-space:pre"></span>return 1;}
执行的结果是:

 open the write-only txtszFileContent:

正确的代码如下:(注释了与另一个指针有关的所有操作)

#include <stdio.h>#include <IOSTREAM.H>#include<string.H>int main(){FILE *fRead,*fWrite;char chChar = 'a';char szFileContent[100];int i = 0;fRead = fopen("d:\\1.txt","r+");// fWrite = fopen("d:\\1.txt","wt");memset(szFileContent,0,sizeof(char)*100);if (fRead != NULL){fseek(fRead,0,SEEK_SET); while((chChar = fgetc(fRead))&&(chChar != EOF))           {              putchar(chChar);  szFileContent[i] = chChar ;i++;        }  szFileContent[i] = 0;fclose(fRead);fRead = NULL;}else{cout<<"can't open the txt"<<endl;}// if (fWrite != NULL)// {// fprintf(fWrite," open the write-only txt");// cout<<" open the write-only txt"<<endl;// fclose(fWrite);// fWrite = NULL;// } // else// {// cout<<"can't open the txt"<<endl;// }cout<<endl<<"szFileContent:"<<szFileContent<<endl;return 1;}
执行结果是我想要的:

szFileContent: open the write-only txt open the write-only txt


0 0
原创粉丝点击