C++中怎么从文件中读取多个字符串?

比方说a.txt中存储多个单词,每个单词间以空格分开。
想读进一个string a【10】的数组中
第一个单词存在 string a【0】,第二个存在string a【1】以此类推?
急急~~

第1个回答  2010-06-07
#include <stdio.h>

int main(void)
{
char a[10][80]; int i = 0;
for (i = 0; i < 10; i++) scanf("%s", a[i]);
for (i = 0; i < 10; i++) printf("第%d个单词是%s\n", i, a[i]);
return 0;
}

把上面的工程编译成可执行程序myword.exe,然后执行:
c:\myword\debug\myword < a.txt
就知道结果了,你看看?
第2个回答  2010-06-07
#include <stdio.h>
#include <string>
int main()
{
freopen( "a.txt" , "r" , stdin) ;
string a[100] ;
int i = 0 ;
while ( cin >> a[i] ) cout << a[i++] << endl ;
return 0;
}本回答被提问者采纳
第3个回答  2010-06-07
#include <fstream>
#include <string>

using namespace std;

int main(int argc,char** argv){
ifstream in("a.txt");
string a[10];
if(in){
for(int i = 0; i < 10; i++)
in>>a[i];
}
return 0;
}
第4个回答  2010-06-08
这个可多了
gets()
getline()
等等
你要详细的吗?
相似回答