c++中如何从 txt 文件中整行读入字符 至几个字符串数组中

A.txt文件中为
Hello/n
world/n
打开文件text.open(A.txt,ios::in)

有结构的定义
struct a
{
int id;
content char[];
}

现在定义连个对象 a x,y
那么如何将A中的两行读到x,y的content中呢
即x.content=hello
y.content = world

求助高手,谢谢啦

第1个回答  推荐于2016-02-17
//CStdioFile 是MFC的类
CStdioFile file;
if(!file.Open(path,CFile::modeRead))
{
AfxMessageBox(_T("打开文件失败!"));
return FALSE;
}

CSrtintg temp;
while (file.ReadString(temp))
{
....
temp里面就是读出来的一行,再把它保存到你的数组中
}
file.Close();本回答被提问者和网友采纳
第2个回答  推荐于2017-10-15
string* line;////存放字符串数组
ifstream stream;
int i=0;
stream.open("xx.txt");
while(getline(stream,line[i]))
i++;
stream.close();
第3个回答  2010-12-09
http://hi.baidu.com/sx_liang/blog/item/254d98e98336065e79f05579.html
额。。。这个是我的博客地址,,里边有C++ 文件读写操作。。。比较简单,用的是C++ STL 的一些东西,,你看看是不是感兴趣。。。

期待继续交流。。。
相似回答