C++ reading from file problem

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

Cheers,
SK
Last edited on
In pseudo-code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


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() )    */
file.getline( Line, sizeof( Line ) - 1 )
file.getline( Line, sizeof( Line ) )

http://www.cplusplus.com/reference/iostream/istream/getline/
Thanks Syuf, I always forget that getline throws the newline away.
It's not becuase it throws the delimeters, but it reads n-1 characters and adds '\0' automatically.
Thanks, I'll have to experiment on this. Cheers!
To be complete:

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.


Thanks again!

Good luck Saikitl!
Topic archived. No new replies allowed.