I'm making a program that is essentially an itunes library. There is a text file to import in the format of:
1 2 3 4 5 6 7 8 9 10 11 12 13
ID=Number (long)
Title="SongTitle"
Artist="ArtistName"
FileSize=Size(long)
Date = DateAdded
ID=Number2 (long)
Title="SongTitle2"
Artist="ArtistName2"
FileSize=Size2(long)
Date = DateAdded2
cont. with more songs
I've got code working that correctly uses my MusicEntry struct. I'm storing the information in a vector and my goal is to have it clear the vector and "start over" when it detects a blank line. However, it's only working for the first song in the text file and I'm not sure why. I have it set to a while loop until the end of the file was reached. I also thought maybe the blank line between songs was causing some sort of problem so I tried getting rid of it and it gave the same result.
MusicLibrary::MusicLibrary(string f)
{
vector<string> song_info;
ifstream song_file;
song_file.open(f);
while(!song_file.eof())
{
string line;
getline(song_file, line);
istringstream iss(line);
string right = right_side(line); //Gets the information from right side of = sign in txt file.
song_info.push_back(right); // Adds the new information to a vector
}
//THIS STUFF BELOW (UNTIL FUNCTION RIGHT_SIDE) IS ALL RELATED TO THE VECTOR SONG_INFO AND CREATING A MUSIC ENTRY.
long song_ID = strtol(song_info[0].c_str(), NULL, 0);
string name = song_info[1]; //This stuff pulls the info. from the vector and attaches it to MusicEntry string names
string artist = song_info[2];
string file_size = song_info[3];//Converts the file size string into a long so the MusicEntry will accept it
long size = strtol(file_size.c_str(), NULL, 0);
string date_added = song_info[4];
MusicEntry song(name, artist, size, date_added); //Creates a new song using the above information
Music_List[song_ID]= song; //Adds the current song to the MusicLibrary map
cout << song.to_string() << endl; //Prints the correct MusicEntry for the song.
song_info.erase(song_info.begin(), song_info.end()); //Clears the Vector and re-sets everything for the next song.
}
string right_side(const string &line) //Function
{
unsigned pos = line.find("=");
string right_side = line.substr(pos +1);
return right_side;
}
Any ideas where/why it's getting stuck and stopping after the first batch of info. in the text file? Thanks in advance.
Any ideas where/why it's getting stuck and stopping after the first batch of info. in the text file?
It doesn't. You use just the first strings -> a loop is missing
The while loop isn't good. I don't see any use of line 11. And you're adding an extra line at the end (because you check eof too late). Better write it like so:
1 2 3 4 5 6 7 8
string line;
while(getline(song_file, line)) // this will prevent to add an extra line in case of an error (not only eof)
{
string right = right_side(line); //Gets the information from right side of = sign in txt file.
song_info.push_back(right); // Adds the new information to a vector
}
while(!song_file.eof())
{
string line;
getline(song_file, line);
istringstream iss(line);
string right = right_side(line);
song_info.push_back(right);
cout << right << endl; /// add a cout here and you will see that the whole file is read.
}
You need to include all below //THIS STUFF BELOW... in the while loop but make it conditional on line =="" or size of the vector == 5.
Also make sure line being empty string i.e. "" doesn't cause problems in any of your functions. line will be empty when getline reads an blank line.
Been messing with counters/loops for a while with no luck. It seems like any time I move the stuff under //THIS STUFF BELOW... it either only reads the first line or none at all.
I've got the following to work and correctly read all of the info. for the first song:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
MusicLibrary::MusicLibrary(string f)
{
vector<string> song_info;
ifstream song_file;
song_file.open(f);
while(!song_file.eof())
{
string line;
getline(song_file, line);
string right = right_side(line); //Gets the information from right side of = sign in txt file.
song_info.push_back(right); // Adds the new information to a vector
cout << right << endl;
MusicLibrary::MusicLibrary(string f)
{
vector<string> song_info;
ifstream song_file;
song_file.open(f);
while(!song_file.eof())
{
int cnt = 0; //Sure this might be a problem because each time it takes a line it resets to 0, yes?
string line;
getline(song_file, line);
string right = right_side(line); //Gets the information from right side of = sign in txt file.
song_info.push_back(right); // Adds the new information to a vector
cout << right << endl;
if(cnt = 5)
{
//Stuff from "//THIS STUFF BELOW" here and cnt = 0;
}
cnt++;
}
}