ifstream fin("student.txt");
int size; //size of students
int count;
fin >> size;
int i=0;
for(int i=0;i<2;i++)
{
string name, gender, phone;
fin >> count >> name >> gender >> phone ;
student[i].setName(name);
student[i].setGender(gender);
student[i].setPhoneNumber(phone);
}
my student.txt is something like this
1 2 3
2
0 aaa male 123456
1 bbb male 345678
How to load the data inside the txt files and reput into the program as objects of classes? I tried the code above without any compilation error but it has runtime error. everything runs normally if I didn't call class member to set my data inside my vector which consists of students information.
for(int i=0;i<2;i++) // <- is this meant to be a const 2 or size?
{
string name, gender, phone;
fin >> count >> name >> gender >> phone ;
// try looking inside these methods as very thing that you've given should run fine.
student[i].setName(name);
student[i].setGender(gender);
student[i].setPhoneNumber(phone);
}
Your really not giving enough information for any one to truly pin down the error. Is your runtime error giving you any information other then just crashing?
EDIT: You also might want to check that the program is and can actually open your const file name before trying to get data from it. if(fin.fail()) { /* it failed! */}
it is the size that I get from the previous input, 2 is to check whether the loop is working or not,
I found that loop is working fine without calling update class members,
that means fin >> count >> name >> gender >> phone ; is working fine.
note:
1. this loadData function is in different cpp files because I'm doing seperate compilation
2. phone number is something I should get by doing my exercise, so I assigned every phone numbers to be "None".
I suppose the result of using initializer list is the same thing as the way I have assigned those values.
fstream fin;
fin.open("students.txt", ios::in);
if(fin.fail()) {
// throw error here
cout << "Error: Could not open file." << endl;
return;
}
int size; //size of students
int count;
// I'm guessing on this part so fix it how you want
fin >> size;
for( ; count < size; count++) {
string name, gender, phone;
fin >> count >> name >> gender >> phone ;
student[count].setName(name);
student[count].setGender(gender);
student[count].setPhoneNumber(phone);
}
If by "they" you meen the .exe (or working directory from which it's executing) and the save file, then yes. Also are you closing the stream after saving? Are you sure the data is in there and in the right format?
bbgst: Thanks for your information, but I think I don't need complicated thing because this is my assignment and I want my txt not only readable in my pc but also in my lecturer's pc as well..
how to code with ifstream::is_open ?
sloppy9: Yea I found that. Used back my file name, at least I didnt show up fin.fail message, but the program still didn't loaded with any data inside my file =(
My declaration of student vector is inside my main folder: vector<Student>student;
my loadData function is using pass by reference vector<Student>& student
My declaration of student vector is inside my main folder:
vector<Student>student;
There's the problem.. you have created an empty vector, student[0] and student[1] do not exist.
You could specify the number of elements in the student vector constructor vector<Student> student(size); and iterate over the vector as you are, or use the vector::push_back() function to insert new elements.
sloppy9: Get it! Thanks.
Suddenly forgot student should be a vector but not a dynamic array, so I forgot to push_back!
I just push back my data and everything works fine!
fstream fin;
fin.open("student.txt", ios::in);
if(fin.fail()) {
// throw error here
cout << "Error: Could not open file." << endl;
return;
}
int size; //size of students
fin >> size;
for(int count =0; count<size; count++)
{
string name;
int gender, phone,postalcode;
int something;
fin >> something >> name >> gender >> phone >> postalCode ;
student.push_back(Student(name,gender,phone,postalCode));
}
fin.close();