Hi, basically I have to compile a program that reads from a .txt file and then performs certain task. My problem now is that my .txt file contains many lines with spaces, and I need to differentiate the items in each line
e.g. abc.txt has lines such as:
Name Type Date
Name1 Type1 Date1
Name2 Type2 Date2
etc
How can I use getline to get Name, Type and Date?
Would I have to include a delimiter that is the end of each line? Would it be getline(.txt, Name, '/n')?
Also, I was googling and stumbled upon nested getlines but could not find an example?
Or should I not even be using getline? In which case tragedy strikes
char Line[ 256 ];
ifstream file( "yourfile.txt", "r" );
if( file.good() )
{
while( ! file.eof() )
{
file.getline( Line, sizeof( Line ) - 1 );
if( file.good() )
{
/*
You now have the contents of a line in your variable named "Line". You will need to
loop through the Line variable and find ieach item and take appropriate action.
*/
} /* if( file.good() ) */
} /* while( ! file.eof() ) */
Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.
Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it. If you don't want this character to be extracted, you can use member get instead.
The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.