すでに存在するこのテキストファイルを読まなければなりません。このコードはコンパイルして機能しますが、1行に1ワードしか読み込まれません。
例:私のtxtファイルは次のようになります:
- word1 word2 word3 word4
- word5 word6 word7
- word8 word9
ただし、画面に出力されます。
- word1
- word2
- word3..など
スペースを含む配列にtxtファイルを読み込むにはどうすればよいですか?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string friendConnections[9];
string line;
int loop = 0;
ifstream networkFile("network.txt");
if (networkFile.is_open())
{
while (!networkFile.eof())
{
istream& getline (networkFile >> line);
friendConnections[loop] = line;
cout << friendConnections[loop] << endl;
loop++;
}
networkFile.close();
}
else cout << "Can't open file" << endl;
return 0;
}