Learning to use direct access in .txt. Help!

I am trying different ways to change data in a .txt file by using direct access which I have not used before. Here is the 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
#include <iostream>
#include <fstream>

using namespace std;

int main() {

	fstream fs("f.txt", ios::in | ios::out | ios::trunc);
	streampos pos = fs.tellg();
	char c = '\0';
	fs << "This is row 1." << endl
		<< "This is row 2." << endl
		<< "This is row 3." << endl;

	//Output content of file f.txt before changes.
	fs.seekg(0, ios::beg);
	while(fs.get(c))
		cout << c;
	fs.clear();

	//Change content in file f.txt by using direct access.
	fs.seekp(0, ios::beg);
	fs.seekg(0, ios::beg);
	while(fs.get(c))
		fs.put('*');
	fs.clear();

	//Output content of file f.txt after changes.
	fs.seekg(0, ios::beg);
	while(fs.get(c))
		cout << c;
	fs.clear();

	system("pause");
	exit(EXIT_SUCCESS);
	return 0;
}


In the second step, when I try to overwrite old characters with new '*'-characters, nothing happens, which I see after the second time that I output the file content through the standard output. Why does it not work?

Last edited on
closed account (o3hC5Di1)
Hi there,

I couldn't tell what's wrong by looking at the code, but this would be a good chance for you to start using a debugger.
Debuggers are used to troubleshoot programs which do compile, but behave unexpectedly.

If you use an IDE like code::blocks or Visual Studio, there is a debugger available within the IDE.
You can even set breakpoints at the function that changes the files content and follow the process step by step from there.

All the best,
NwN
Thank you, I use Visual studio 2010 and I will try using the breakpoint and try do debug it. Any others who can spot the problem, please tell me.

Well I found the problem but do not know why it occurs. It says: "Could not find the PDB file." when debugging the folowing code:

1
2
3
4
5
6
7

fs.seekp(0, ios::beg);
	fs.seekg(0, ios::beg);
	while(fs.get(c))
		fs.put('*');
	fs.clear();


I read that it has to do with Visual C++ 2010 that some dll file is missing but why this error and how do I solve it? I still want to use VC :/ ?
Last edited on
Topic archived. No new replies allowed.