读取文件内容异常,请问原因是什么(visual stdio 2017)
#include
#include
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
//char str;
FILE *F = fopen("test.txt", "r+");//fopen("文件名(文件路径+文件名主干+文件后缀)"//返回的是文件信息区的首地址
char ch;
while(!feof(F))
{
char ch=fscanf(F,"%c",&ch);//从F指向的文件中读一个字符
cout<<(char)ch;
}
fclose(F);//fclose(文件指针)文件关闭成功返回0否则返回EOF(-1)
system("pause");
return 0;
}
char ch = fscanf(F,"%c",&ch)改为
fscanf(F,"%c",&ch);
不要再定义一个ch变量了。而且fscanf的返回值是读取到的值得数量,这个ch等于1啊,当然乱码
if (!F){
cout << "can't open the txt file." << endl;
return -1;
}
在while前面加个判断看看,是不是路径不对