c++ - How do I correctly operate tellg() and seekp() to accomplish this task? -
void edit_member() { int id; int slot; streampos pos; cout << "\n" << "enter member id#: "; cin.ignore(); id = only_int(); int mems = count_members(); (int = 0; < mems; a++) { if (id == member[a].id) { i_file.open("member_data.txt"); (int b = 0; b <= a; b++) { string void_data; getline(i_file, void_data, '#'); pos = i_file.tellg(); slot = a; } i_file.close(); break; } } o_file.open("member_data.txt"); cout << "\nenter new name id# " << id << ": "; cin.ignore(); getline(cin, member[slot].name, '\n'); o_file.seekp(pos) << member[slot].name; o_file.close(); load(); }
i trying navigate specific point in file , overwrite name written there beside corespondent id#. thought seekp() similar positioning cursor in text editor, had done attempt position cursor @ end of name , "fill" '\b' (backspace) length of name, equivalent
member[slot].name.length()
i removed bit of code advanced cursor far, , abandoned backspace idea. i'm trying put 'cursor' @ beginning of name , write on it. want make sure if new name shorter old one, ends of old 1 aren't still present in file.
currently happens entire file erased , new name present @ beginning of file. i'm self taught , still pretty basic, assistance appreciated.
writing file not typing in notepad or other file editor. when put string won't push rest, overwrite old text.
if values doesn't have same size you'll need write rest of file after change. if want avoid it, need keep empty places (spaces) size , limit input same width. , when write new value give same width.
you need open out file read/write, otherwise truncated.
edit:
//when open file in write mode, content deleted. //to keep old file content, need open both read , write: fstream outfile; outfile.open ("test.txt",ios::in|ios::out);
Comments
Post a Comment