请问如何将txt文档内容加载到C++的字符串变量X中?

2025-02-26 05:52:28
推荐回答(4个)
回答1:

void ReadFromFiletoString()
{
ifstream fin("data.txt",ios::in ); //创建输入文件流对象
string x;
while ( getline(fin,x) ) //从文件流中逐行读取字符串到x
{
cout << " Read from file: " << x << endl;
}
}

回答2:

char cstr[6]="";
FILE *fp = fopen("C:\\1.txt","wr+");//打开一个文件
scanf(fp,"%5s",cstr);//从fp指向的文件流读5字节长的字符串赋给cstr

回答3:

使用文件打开建立fostream() 用fopen赋予X,

回答4:

使用fopen_s()比较简单