C文件操作以a +模式打开文件 [英] C file operation open file in a+ mode
问题描述
大家好,
在"a +"模式下打开文件时遇到一些问题.根据a +模式.它应打开以进行读写(如果文件存在,则追加),并从文件末尾开始.但是,每当我检查EOF时,它就会断言.请检查以下代码.
FILE * pFILE = fopen(" ," );
ASSERT( 0 != feof(pFILE));
fclose(pFILE);
pFILE = 0 ;
有人可以帮助我理解这一点吗?
在此先感谢.
在断言之前尝试fseek ( pYFILE ,0 , SEEK_SET );.
由于它也处于追加模式,因此文件指针将位于文件的末尾.
不知道YFILE
和yfopen
等的实际作用,这只是猜测而已. .
但是,我不禁想到,仅从名称pYFILE
应该是YFILE*
而不是YFILE
别认为这可以解决您的问题,但是您似乎需要做一些解释...
使用改善问题"小部件来编辑您的问题并提供更好的信息.
FILE *pFILE = fopen("c:\\TEST.txt", "a+"); ASSERT(0 != feof(pFILE));
特征 [
您尚未执行读取操作,因此feof将返回零.如果文件存在).
因此:
1)打开文件以读写追加模式.
2)检查EOF是否...似乎缺少一些代码.还:您确定它是
而不是feof
3)关闭流.
4)清空指针.
我会在while循环中使用ASSERT,否则此代码似乎毫无意义.
例如
while(ASSERT(0!= feof(pYFILE)))
{
count = fread(buffer,sizeof(char),100,stream);
总数+ =计数;
}
Hi Everyone,
I am facing some problem while opening a file in " a+" mode. according to the a+ mode. it should open for reading and writing (append if file exists) and starts at the end of file. but when ever i am checking for EOF it Asserts. please check the below code.
FILE *pFILE = fopen("c:\\TEST.txt", "a+");
ASSERT(0 != feof(pFILE));
fclose(pFILE);
pFILE = 0;
can anybody help me in understanding this.
Thanks in advance.
Tryfseek ( pYFILE ,0 , SEEK_SET );before assert.
As it is in append mode as well the file pointer will be at the end of the file.
Without knowing whatYFILE
andyfopen
etc. actually do, it is just guesswork.
But, I can''t help thinking that just from the namespYFILE
should be aYFILE*
rather than aYFILE
Don''t think that will fix your problem, but it looks like you have some explaining to do...
Use the "Improve question" widget to edit your question and provide better information.
FILE *pFILE = fopen("c:\\TEST.txt", "a+"); ASSERT(0 != feof(pFILE));
feof[^] "The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file."
You haven''t done a read operation, so feof will return zero.
The code mearly cleans up after opening the file for read and write mode (append if the file exist).
Thus:
1) open the file for reading and writing append mode.
2) Check whether EOF... Looks like some code missing. ALSO: are you sure its yfeof
and not feof
3) close the stream.
4) Nullify pointer.
I would use the ASSERT in a while loop otherwise this code seems pointless.
E.g.
while ( ASSERT(0 != feof(pYFILE)) )
{
count = fread( buffer, sizeof( char ), 100, stream );
total += count;
}
这篇关于C文件操作以a +模式打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!