I'm currently writing part of a lab program for a data structures class I'm taking. I'm only partway into the lab, so there's not much written at the moment.
First, let me apologize in advance. This code will be a bit messy, as the last two CS classes I had were not in C++, and it's been almost a year since I've coded much of anything (last semester's class was Computer Organization, taught with MIPS32 and such).
I'm encountering a seg fault. The gdb debugger's report on the segfault is as follows:
Code:
Program received signal SIGSEGV, Segmentation fault.
__strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:31
31 ../sysdeps/x86_64/multiarch/../strlen.S: No such file or directory.
in ../sysdeps/x86_64/multiarch/../strlen.S
I have narrowed down the search to what seems to be the problem code, but I don't see anything wrong with it... The problem code, and the related variable, are underlined.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <cstdlib>
#include <iomanip>
using namespace std;
class Song{
public:
string title;
int time;
int track;
};
class Album{
public:
map <int, Song*> songs;
string name;
int time;
};
class Artist{
public:
map <string, Album*> albums;
string name;
int time;
int nsongs;
};
int main(int argc, char* argv[])
{
ifstream fin;
string filename;
int min;
int sec;
int time;
size_t found;
string s;
Song* curSong;
filename = argv[1];
if(argc == 2 && filename.find(".txt") != string::npos){
fin.open(argv[1]);
if(fin.fail()){
cerr << "Problem opening " << filename << endl;
exit(1);
}
}
else{
cerr << "Please use format: lib_info filename.txt\n";
exit(1);
}
while(!fin.eof()){
fin >> s;
for(found = s.find("_"); found != string::npos; found = s.find("_")) {
s.replace(found, 1, " ");
}
curSong->title = s;
fin >> s;
sscanf(s.c_str(), "%d:%d", &min, &sec);
curSong->time = min*60 + sec;
fin >> s;
fin >> s;
fin >> s;
fin >> s;
sscanf(s.c_str(), "%i", &(curSong->track));
}
return (0);
}
I'm unfamiliar with s.find and string::npos. I pulled them off a reference site, and attempted to use them, so I wouldn't be surprised if that's where the problem was.
In case it's needed, here's a sample input file.
Code:
Countdown 2:25 Coltrane,_John Giant_Steps Jazz 3
Down_In_Brazil 6:07 Walton,_Cedar Naima Jazz 4
Giant_Steps 4:02 Puente,_Tito El_Rey Jazz 5
Giant_Steps 4:46 Coltrane,_John Giant_Steps Jazz 1
Mr._P.C. 7:02 Coltrane,_John Giant_Steps Jazz 7
Naima 4:24 Coltrane,_John Giant_Steps Jazz 6
Naima 5:16 Lyle,_Bobby Night_Breeze Jazz 5
Naima 5:36 Tjader,_Cal A_Fuego_Vivo Jazz 6
Naima 7:49 Walton,_Cedar Naima Jazz 6
Naima 8:38 Walton,_Cedar Eastern_Rebellion Jazz 2
This_Guy's_In_Love_With_You 8:10 Walton,_Cedar Naima Jazz 2