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
- error:fgetc函数无法获取文本文档中的内容
- fckeditor jquery serialize 无法获取文本内容
- 如何提取html文档中的文本内容
- 无法获取 的内部内容,因为该内容不是文本
- asp.net无法获取kindeditor文本编辑器内容的解决办法
- asp.net无法获取kindeditor文本编辑器内容的解决办法
- fgetc函数
- 用iTextSharp读取PDF格式文档中的文本内容
- js中获取匿名函数调用的callee和本js匿名函数中的所有文本内容
- .net webapi控制器函数中与MVC控制器函数中,获取到请求中的文本内容和文件流内容的写法
- 一个获取元素文本内容的通用函数
- 需求:获取其他程序中的ListView控件的文本内容
- 如何获取span中的文本内容与删除
- 无法获取div的内部内容,因为该内容不是文本(VB.net版)
- 报错:InnerHtml无法获取内部内容,因为该内容不是文本。
- 报错InnerHtml无法获取内部内容,因为该内容不是文本。
- php中的file_get_contents函数------获取文件内容
- C#读文本文档内容
- uva10561 - Treblecross SG定理
- 数据库中视图的作用
- C++中delete和delete[]的区别
- poj 2051 Argus(优先队列)
- leetcode Jump Game II
- error:fgetc函数无法获取文本文档中的内容
- DDL引发的对象invalidation
- 【android开发笔记】如何让ImageButton去掉白色边框和让ImageButton具有点击效果
- 从头开始写项目Makefile(零):前言
- c++ 高精度模板
- HDU2222 AC自动机入门
- Ubuntu 使用crontab定时任务
- 深入Java集合学习系列:LinkedHashMap的实现原理
- 21天学通Java学习笔记-Day13(javascript-ajax)