0

For some reason the full lines from my input file are not reading into the array, only the first word in each line. I am currently using the getline call, but I am not sure why it is not working. Here is the what I have for the call to populate the array. The txt file is a list of songs.

const int numTracks = 25;
string tracks[numTracks];
int count = 0, results;
string track, END;

cout << "Reading SetList.txt into array" << endl;

ifstream inputFile;
inputFile.open("SetList.txt");
while (count < numTracks && inputFile >> tracks[count])
{
    count++;
    getline(inputFile, track);
}
inputFile.close();
1
  • You aren't doing anything with track inside the loop. Commented Jul 27, 2016 at 2:02

2 Answers 2

2
while (count < numTracks && inputFile >> tracks[count])

The >> operator reads a single word. And this code reads this single word into the vector in question.

getline(inputFile, track);

True, you're using getline(). To read the rest of the line, after the initial word, into some unrelated variable called track. track appears to be a very bored std::string that, apparently, gets overwritten on every iteration of the loop, and is otherwise completely ignored.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there anyway to change it so that it reads the full line. I have tried putting in tracks[count] instead of track and I get a catastrophic failure of the program every time.
Of course there is, but "a catastrophic failure" is not a useful problem description.
0

Your loop is using the operator>> to read the file into the array. That operator reads one word at a time. You need to remove that operator completely and use std::getline() to fill the array, eg:

const int numTracks = 25;
std::string tracks[numTracks];
int count = 0;

std::cout << "Reading SetList.txt into array" << std::endl;

std::ifstream inputFile;
inputFile.open("SetList.txt");
while (count < numTracks)
{
    if (!std::getline(inputFile, tracks[count])) break;
    count++;
}
inputFile.close();

Or:

const int numTracks = 25;
std::string tracks[numTracks];
int count = 0;

std::cout << "Reading SetList.txt into array" << std::endl;

std::ifstream inputFile;
inputFile.open("SetList.txt");
while ((count < numTracks) && (std::getline(inputFile, tracks[count]))
{
    count++;
}
inputFile.close();

Alternatively, consider using a std::vector instead of a fixed array, then you can use std::istream_iterator and std::back_inserter to get rid of the manual loop completely:

class line : public std::string {}

std::istream& operator>>(std::istream &is, line &l)
{   
    return std::getline(is, l);
}

...

std::vector<std::string> tracks;

std::cout << "Reading SetList.txt into array" << std::endl;

std::ifstream inputFile;
inputFile.open("SetList.txt");

std::copy(
    std::istream_iterator<line>(inputFile),
    std::istream_iterator<line>(),
    std::back_inserter(tracks)
);

inputFile.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.