Hello, I am trying to read a text file using 'fstream' and writing to it at the same time.
- My text file contains the names of all the 12 months in the 3 letter format i.e. Jan, Feb, etc. and there is one empty line after each month
- Under these months I am storing a person's information, i.e. Last name and first name (in same line) and date in the next line and then another empty line
- My function reads this file and its purpose is to input a new name with the date but in alphabetical order under a specific month.
** I have figured out how to detect what month I am going to store under, but I am having trouble when it comes to writing the new name and date, right now it finds the month that it has to go under and writes the record of name and date, but erases the other stuff under (i.e the names of other months that were written before)**
??Therefore how do I read a next line without using getline and then write between the current line and the next line WITHOUT losing/erasing the next lines' data??
Here is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
while (!stored) {
if ((PersonInfo.eof()) && (!stored)){
cout << "Errror: Reached end of file, did not store person's record!" << endl;
return(false);
}
getline(PersonInfo, current_line);
if (current_line == month){
cout << "Found month" << endl;
cout << month << endl;
//PersonInfo.flush();
getline(PersonInfo, current_line);
cout << current_line << endl;
getline(PersonInfo, current_line);
cout << current_line << endl;
if (current_line == next_month) {
PersonInfo.seekp(0, ios::cur);
cout << "Storing record..." << endl;
cout << l_name << ", " << f_name << endl;
cout << day << " " << month << " " << year << endl;
PersonInfo << l_name.data() << ", ";
PersonInfo << f_name << endl;
PersonInfo << day << " " << month << " " << year << endl << endl;
if (!PersonInfo.good()) {cout << "An error occured while trying to write to file!" << endl;}
PersonInfo.close();
if (!PersonInfo.good()) {
cout << "An error occured closing file!" << endl;
return(false);
}
return(true);
}
else {cout << current_line << endl;}
/*while (!found_place) {
pin1 = current_line.find_first_of(",", 0);
getline(PersonInfo, current_line);
}*/
}
else {stored = false;}
}
| |
thankz in advance =)