heskyttberg,
You could try a few things like declare fs as ifstream object.
Dhyanesh
Main Topics
Browse All TopicsHi!
I have a problem with some C++ code, I can't understand what I have done wrong ?
So please point out to me what I'm doing wrong here.
Regards
/Hasse
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include "mp3.h"
using namespace std;
main(int argc, char *argv[]) {
typedef vector<mp3> myMp3Array;
myMp3Array mp3Vec;
fstream fs;
mp3 tmp;
fs.open("test.dat", ios::in | ios::out | ios::binary);
while (! fs.eof()) {
fs.read((char*) &tmp, sizeof(mp3));
mp3Vec.push_back(tmp);
}
for (myMp3Array::iterator it = mp3Vec.begin(); it!=mp3Vec.end(); it++) {
tmp = *it;
cout << tmp.getArtist() << " ";
}
cout << endl;
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi!
After first record is read from file, I get segmentation fault when trying to push it into the vector.
I haven't got a clue as to what I'm doing wrong.
ofstream and ifstream won't help.
The following code works:
// --- START ---
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include "mp3.h"
using namespace std;
main(int argc, char *argv[]) {
typedef vector<mp3> myMp3Array;
myMp3Array mp3Vec;
fstream fs;
fs.open("test.dat", ios::in | ios::out | ios::binary);
mp3 tmp1 ("Jump","Jump","/opt/data/
mp3 tmp2 ("Erotica","Erotica","/opt
mp3 tmp3 ("Crusader","Crusader","/o
mp3 tmp4 ("Jump","Jump","/opt/data/
mp3 tmp5 ("Erotica","Erotica","/opt
mp3 tmp6;
mp3 tmp7;
mp3 tmp8;
mp3 tmp9;
mp3 tmp10;
fs.seekg (0, ios::beg);
fs.write((char*) &tmp1, sizeof(mp3));
fs.write((char*) &tmp2, sizeof(mp3));
fs.write((char*) &tmp3, sizeof(mp3));
fs.write((char*) &tmp4, sizeof(mp3));
fs.write((char*) &tmp5, sizeof(mp3));
fs.close();
fs.open("test.dat", ios::in | ios::out | ios::binary);
fs.read((char*) &tmp6, sizeof(mp3));
fs.read((char*) &tmp7, sizeof(mp3));
fs.read((char*) &tmp8, sizeof(mp3));
fs.read((char*) &tmp9, sizeof(mp3));
fs.read((char*) &tmp10, sizeof(mp3));
fs.close();
mp3Vec.push_back(tmp1);
mp3Vec.push_back(tmp2);
mp3Vec.push_back(tmp3);
mp3Vec.push_back(tmp4);
mp3Vec.push_back(tmp5);
mp3Vec.push_back(tmp6);
mp3Vec.push_back(tmp7);
mp3Vec.push_back(tmp8);
mp3Vec.push_back(tmp9);
mp3Vec.push_back(tmp10);
for (myMp3Array::iterator it = mp3Vec.begin(); it!=mp3Vec.end(); it++) {
tmp1 = *it;
cout << tmp1.getArtist() << " ";
}
cout << endl;
}
// --- END ---
So why dosen't the code where I'm reading from file in a loop work ?
I really don't understand this, hoping for some expert help here.
Regards
/Hasse
Agreed with jkr. Doing a binary write and then a read is equivalent to doing a shallow copy. This has dangers when the structure contains pointers to data allocated outside the class, which is typically the case when you have variable length strings (i.e. char* or std::string).
cout << "My record size is: " << sizeof(mp3) << '\n'; // What about the strings??
If you need to read/write instances of your structure to file, you'll need to write class serialization functions to do it.
A little extra info:
If your "mp3" object contains a std::string, then the data for that string points to someplace in memory where the actual text is stored. When you write that "mp3" raw data to disk, you are saving the pointer-to-text, but not the text itself.
Later, when you read back the "raw data" for that mp3 object, that pointer-to-text will now point to someplace in memory where there is no string data. Thus the problem.
Show the relevant parts of mp3.h ... maybe with a few changes, it might be possible to read and write it as raw data. However, it is usually correct to write serialization functions.
-- Dan
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Split: jkr {http:#9981690} & rstaveley {http:#9983836} & DanRollins {http:#9988038}
Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: dhyaneshPosted on 2003-12-21 at 05:20:01ID: 9981033
Hi heskyttberg,
What error do you get?
Dhyanesh