How to change the specfic file content fstream

source code
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{

	string name, program;
	string name1, program1;
	int id, choise;

	ofstream data;
	ifstream data2;
	
	data2.open("data.txt");
	data.open("data.txt", ios::out | ios::in | ios::app);


	while (true)
	{
		cout << "\nEnter The Student ID : ";
		cin >> choise;
		while (data2 >> id&&data2.ignore(20, '\n'), getline(data2, name) && data2 >> program)
		{
			if (choise == id)
			{
				cout << "\nStudent Id : " << id;
				cout << "\nStudent Name : " << name;
				cout << "\nStudent Program : " << program;
				cout << endl;

				cout << "\nChanging Student Data\n";
				cin.ignore();
				cout << "\nEnter Student Name : ";
				getline(cin, name1);
				cout << "\nEnter The Student Program : ";
				getline(cin, program1);
				cout << "\nEnter The Student ID : ";
				cin >> id;

				data << id << endl;
				data << name1 << endl;
				data << program1 << endl;



			}
		}

		data2.clear();
		data2.seekg(0, ios::beg);

		cout << "\n1 to try again : ";
		cin >> choise;
		if (choise == 1)
		{
			system("cls");
			continue;
		}
		else
			break;


	}
		return 0;
}


text file data

12345
Muhammad Salman Zafar
BS(CS)
12346
Hamza Rizwan
BS(CS)

if i enter 12345
Program Output:
ID : 12345
Name : Muhammad Salman Zafar
Program : BS(CS)

and i want to change this to
Name : What User Enter
Id : What User Enter
Program : What User Enter


Not Change The Whole File Content

plz help

When using an ordinary text file where each field is of variable length, it isn't possible to update the file data 'in-place' unless the new values are of exactly the same length as the values being replaced. Since the length of the name and program (and also possibly the ID) could be different, there is no alternative but to re-write the entire file.

A relatively easy way to do this is to read the entire file into an array or vector. To keep control of the data for each individual student, you might use a structure to hold the data for one student, and create an array of student objects. Modify the values in the array, and write the entire array out to a fresh version of the file.

Another way is to read the data one record at a time (again using a struct could help). If the id matches the requested value, change the data before writing it out. Otherwise write out the unchanged data. Note: this approach avoids the need for storing more than a single student record at a time. However it does require two physically separate files, the original and the updated version.
thnx if i wanted to delete the 12345 user so how can i delete it
if i wanted to delete the 12345 user so how can i delete it

In either of the approaches I outlined, to delete a record, simply don't write it to the output file.

If you store the data for the entire file in a std::vector, you could use the erase() function to delete it from the vector.
http://www.cplusplus.com/reference/vector/vector/erase/

Otherwise, when reading from one file and writing to another, just skip the write for that record.
Last edited on
thnx
Topic archived. No new replies allowed.