谁能看懂这个C语言的程序

来源:百度知道 编辑:UC知道 时间:2024/05/21 20:41:12
#include<iostream>
#include<string>
#include<ctype.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

using namespace std;

//定义读入文件函数
string readfile(){
char fch;
string fstr;
FILE *fp;
if((fp=fopen("cffx.txt","r"))==NULL){
cout<<"不能打开此文件,退出程序!"<<endl;
exit(1);
}
else{
while((fch=fgetc(fp))!=EOF) fstr+=fch; //把从文件中读入的字符连接到fstr后
}
fclose(fp);
return fstr; //返回fstr,即文件中的字符串
}

读取.txt文件中的所有字符到一个字符变量fstr当中。
不过看这个函数,变量fstr接受的字符长度,有可能出问题,因为并不知道文件的大小,而字符变量的长度是有限的,很可能出现超长的情况。