question archive how can we read the following txt document(ifstream) and store in a string array in C++
Subject:Computer SciencePrice:9.82 Bought3
how can we read the following txt document(ifstream) and store in a string array in C++.
abc efg hello
world
test
the string array should be = {"abc","efg","hello", "world", "test"};
I don't know what to do with the extra spaces.
How to a read a txt doc in C++
Step-by-step explanation
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string array[2];
short loop=0;
string line;
ifstream myfile ("doc.txt");
if (mfile.is_open())
{
while (! mfile.eof() )
{
getline (mfile,line);
array[loop] = line;
cout << array[loop] << endl;
loop++;
}
myfile.close();
}
else cout << "can't open the file";
system("PAUSE");
return 0;
}