I am trying to read a text file and store each different word seperately using an array and whitespace as a delimiter. Example: Input from file is "I wish I knew how to do this" and I want to store "I" as word[0], "wish" as word[1], "I" as word[2], "knew" as word[3], "how" as word[4] etc.
Here is what I have so far:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string word[7];
int index = 0;
ifstream myfile("sentance.txt");
if (myfile.is_open())
{
while(! myfile.eof())
{
cin.get(myfile, word[index], ' ');
cout << word[index] << endl;
index++;
}
myfile.close();
}
system("pause");
}
when I compile I get:
.\main.cpp(17) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'std::ifstream' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Any help is greatly appreciated. Thanks in advance for your time.