Hello all, I am a long time browser and first time poster here. Right now I am a little stuck on my project and could use a little help.

The program opens a .txt file and then reads the data and places the data accordingly. The issue this time compared to most times is that the items I am trying to send to a string have spaces in them. (firstname lastname, adress, phone) So, I am not able to use the regular:

inFile >> name[i] >> address[i] >> phone[i];

Is there anyway I can get this information in from the file as a string or do I have to use a c-string? I have also tried what I have below but the artist outputs everything after instead of only until the location of the double space.

while (!inFile.eof())
        pos = 0;                                    // Sets position to start
        inFile.getline(line, 100);                    // Receives first line as full c-string
        tempStr = string(line);                        // Sets line to a temp string

        ds1 = tempStr.find("  ", 0);                // Finds first double space position after title
        title[ptr] = tempStr.substr(pos, ds1);        // Sets title to substring before double space
        pos = 37;                                    // Sets position to artist
        ds2 = tempStr.find("  ", pos);                // Finds first double space position after artist
        artist[ptr] = tempStr.substr(pos, ds2);        // Sets artist to substring before double space

        cout <<  title[ptr] << " " << artist[ptr];    // Test output
        cout << endl;        


Thanks for any help possible.

7 Years
Discussion Span
Last Post by lepinat0r

In the line number 9 I can't understand why you hardcode that to 37 ?



I hard coded it due to how the text file is formatted in columns. I have figured out the problem though. I was used to java and the substr command is (startpos, endpos) and in c++ it is (startpos, len). :\

Thanks anyways.

Edited by lepinat0r: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.