Replacing int type variable in text file

Initially i have a structure array store all of the data from the text file. Now i want to checkout a tool from the text file how do i change the quantity of it. This is what i currently did but it does not change the value inside the text file why is that ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  ifstream inFile;
  inFile.open("tools.txt");
  if (!inFile)
{
	cout << "\nError opening file.";
}
else
{
	for (int i = 0; !inFile.eof(); i++)
	{
		inFile.getline(tools[i].code, 5);
		inFile.getline(tools[i].name, 30);
		inFile >> tools[i].quantity;
		if (i == toolLoc)
		{
			tools[i].quantity--;
			cout << "\nThe tool have been successfully check out.\n";
		}
	}
	inFile.close();
}

This is the text file
1
2
3
4
5
6
101,ladder, 20
102,sawing machine, 15
103,trolley, 10
104,drill, 30
105,toolbox, 50
106,step ladder, 20
Last edited on
You are reading a file in as a stream and parsing strings/ints out of it. These value now exist in the memory of your program. The file itself is left unchanged.

If you want to change the file itself, you need to write the entire file out again, with an ofstream (output) instead of an ifstream (input).

Try it with simple ints/strings first so you understand how it works.

e.g. if file contains
42 fruit


You can do:
1
2
3
4
5
6
7
8
fstream fin("in.txt");

int apple;
string banana;
fin >> apple >> banana;

fstream fout("out.txt");
fout << apple << ' ' << banana << '\n';

Last edited on
im trying to overwrite the text file but is there a way to overwrite the existing text file instead of appending to the text file?
Start the write at the beginning of the file. Use .seekp(0)
yes and no :)
you can replace 100 with 765: both have 3 ascii characters.
you can replace 100 with 0, and have 2 extraneous spaces.
you can replace 100 with 12345678 and overwrite the text that followed the original 100.
see the problems?
its better to read the file up to the number, get the number, then write what you had to a new file, with the new number, and then write the rest of the original file after your new number. Its the "array" problem: how do you add a number in the middle of an array of ints? you move all the values after the insert location down by 1 if you have space (not a problem in a file!) and insert it. There isnt a good way to avoid the shuffle.
You can read in the whole file into say a vector of some struct, modify the vector data as required and then write the vector data back to the file. This way doesn't require the use of a second file. Although if the file is too large to read into memory then using a second file may be a requirement.

Alternatively, depending upon what you need, you can read the while file into say a string, manipulate the string as needed then write the updated string back to the file.

In this case, I suggest reading the file into a vector, modifying it and then writing back. Something like as demo 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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

struct Tools {
	std::string code;
	std::string name;
	size_t qty {};
};

int main()
{
	std::fstream inFile("tools.txt");

	if (!inFile)
		return (std::cout << "Error opening file.\n"), 1;

	std::vector<Tools> tools;

	for (Tools tool; std::getline(inFile >> std::ws, tool.code, ',') && std::getline(inFile, tool.name, ',') && inFile >> tool.qty; tools.push_back(tool));

	bool again {};

	do {
		std::string code;
		size_t qty {};

		std::cout << "Enter tool code: ";
		std::getline(std::cin, code);

		if (const auto itr {std::find_if(tools.begin(), tools.end(), [code](const auto& t) {return t.code == code; })}; itr != tools.end()) {
			std::cout << itr->name << "  " << itr->qty << '\n';
			if (itr->qty > 0) {
				std::cout << "Enter quantity to check out: ";
				std::cin >> qty;

				if (qty > itr->qty)
					std::cout << "Only " << itr->qty << " can be checked out\n";

				itr->qty -= std::min(qty, itr->qty);
			}
		} else
			std::cout << "Code not found\n";

		std::cout << "Again (1 yes, 0 no): ";
		std::cin >> again;
		std::cin.ignore();
	} while (again);

	inFile.clear();
	inFile.seekp(0);

	for (const auto& [code, name, qty] : tools)
		inFile << code << ',' << name << ", " << qty << '\n';
}



Last edited on
Topic archived. No new replies allowed.