#include "stdafx.h"
#include "string.h"
#include "stdio.h"
void CharReadWrite(FILE *fin);
FILE *fptr2;
int _tmain(int argc, _TCHAR* argv[])
{
char filename[]="ReadMe.txt";
if (fopen_s( &fptr2, filename, "r" ) != 0 )
printf( "File stream %s was not opened\n", filename );
else
printf( "The file %s was opened\n", filename );
CharReadWrite(fptr2);
fclose(fptr2);
return 0;
}
void CharReadWrite(FILE *fin){
int c;
while ((c=fgetc(fin)) !=EOF) {
putchar(c);}
}
答案 0 :(得分:3)
您正在打开ReadMe.txt进行撰写:
fopen_s( &fptr2, filename, "w" )
然后将其传递给CharReadWrite
:
CharReadWrite(fptr2);
试图从中读取:
c=fgetc(fin)
您是否打算将"r"
传递给fopen_s
来打开它进行阅读?
如果打开ReadMe.txt失败,您也会继续并致电CharReadWrite
。
答案 1 :(得分:1)
一个问题是,即使您未能打开它,也会尝试阅读(然后关闭)fptr2
。
尝试:
#include "stdafx.h"
#include "string.h"
#include "stdio.h"
void CharReadWrite(FILE *fin);
FILE *fptr2;
int _tmain(int argc, _TCHAR* argv[])
{
char filename[]="ReadMe.txt";
if (fopen_s( &fptr2, filename, "w" ) != 0 )
{
printf( "File stream %s was not opened\n", filename );
}
else
{
printf( "The file %s was opened\n", filename );
CharReadWrite(fptr2);
fclose(fptr2);
}
return 0;
}
答案 2 :(得分:0)
问题在于这一行:
if(fopen_s(& fptr2,filename,“w”)!= 0)
相反,请使用“r +”标记。
答案 3 :(得分:0)
你正在打开文件写作并尝试从中读取(fgetc)......