0

I am reading from a text file that looks like this:

1|Pink Floyd
2|Genesis
3|Einaudi
4|Melanie C

I have a vector artistVector to store the ID (integer), and the Artist Name char(100).

I am trying to parse the text file and store the parsed ID and Name like this:

artistVector[0] = 1
artistVector[1] = Pink Floyd
artistVector[2] = 2
artistVector[3] = Genesis
artistVector[4] = 4
artistVector[5] = Einaudi
artistVector[6] = 6
artistVector[7] = Melanie C

But for some reason, my code after Pink Floyd is not working with my sqlite insert record function.

static vector<string> readFile(vector<string> artVec, ifstream &iFile) //Read the file into the vector function definition
{
vector<string> vector;
string line;
if (iFile.is_open()) {
    while (!iFile.eof()) {
        // read an item using | as a delimiter
        //getline(artist, line, '|');
        getline(iFile, line, '|');
        vector.push_back(line);

        getline(iFile, line, '\n');
        vector.push_back(line);
        getline(iFile, line, '\n');
        vector.push_back(line);
    }
}
else {
    cout << "Unable to open file";
}
return vector;
}

int main() {

vector<string> artistVector;
ifstream artistFile("artist.txt"); //Input file

artistVector = readFile(artistVector, artistFile);
std::string sqlArtistInsert[8];

for (int i = 0; i < artistVector.size(); i++) {
    sqlArtistInsert[i] = artistVector[i];
}
cout << sqlArtistInsert[0] << endl;
cout << sqlArtistInsert[1] << endl;
cout << sqlArtistInsert[2] << endl;



system("pause");
return 0;
}

My output looks like this:

1
Pink Floyd

2

Notice the white space between Pink Floyd and 2? aka sqlArtistInsert[1] & sqlArtistInsert[2]

please take a look at my code below and any help would be much appreciated!

BarcodePiglet
  • 119
  • 1
  • 8

1 Answers1

0

Problems I see:

Problems in while loop

while (!iFile.eof()) {
    // read an item using | as a delimiter
    //getline(artist, line, '|');
    getline(iFile, line, '|');
    vector.push_back(line);

    getline(iFile, line, '\n');
    vector.push_back(line);
    getline(iFile, line, '\n');
    vector.push_back(line);
}

The last call to getline will read an additional line of text. When the loop is executed the first time,

line will be assigned "1" in the first call to getline.
line will be assigned "Pink Floyd" in the second call to getline.
line will be assigned "2|Genesis" in the third call to getline.

I am sure that is not your intention. Remove the third call to getline in the loop.

The other problem is that you will end up storing more items in vector than there are in the file. See Why is iostream::eof inside a loop condition considered wrong? for a more in-depth explanation. Your loop needs to be something like:

  while (getline(iFile, line, '|')) {
     vector.push_back(line);

     getline(iFile, line, '\n');
     vector.push_back(line);
  }

Insufficient size of sqlArtistInsert in main

With your current size of 8, the program was using memory beyond the valid limits due to the problems in the while loop. With the while loop fixed, that should not be a problem any more.

To be safe, I would use:

for (unsigned i = 0; i < artistVector.size() && i < 8; i++) {
   sqlArtistInsert[i] = artistVector[i];
}
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270