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

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 txt

szFileContent:

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

#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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值