Function has a bug under only 1 condition

Hello,

I am munipulating a text file for my c++ course.

The software replaces characters in a text file.
The only problem is that if I replace a number óf characters beginning with the first character in the file, the output gets distorted.

In the textfile

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890

gets changed to (The * stands for a space)
**********12345678901234567890
123456789012345678901234567890
123456789012345678901234567890

but on the console the thing is shown as

12345678901234567890
123456789012345678901234567890
123456789012345678901234567890

this is the function to read the file to the screen:

1
2
3
4
5
6
7
8
9
10
11
12
void Daten::lesen()
{	
	ifstream MyIfstream(DateiName);
	char ch;
	MyIfstream >> ch;
	while (!MyIfstream.eof())
	{
		cout << ch;
		ch = MyIfstream.get();
		
	};
};

Why do I not get the 10 spaces printed to the console?

int main
Last edited on
Is there another function that changes the file? We would need to see that one as well to compare.
I have the code at home so I will provide it this evening (CET).

The thing is, though, that the file is changed correctly. If I open it with WordPad, the spaces are in the correct location and the formatting is as it should be. Only on the console-"printout" the first spaces somehow get lost .

int main
Here is the code of the function which manipulates the file.

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
void Daten::manipulate(int Pos, int Amount, char Overwrite)
{
	fstream MyFstream(FileName);
	Pos = Pos - 1; // c++ starts counting with 0
	char ch;
	for (int i=0;i<Pos;i++)
	{
		ch = MyFstream.get();
		if (MyFstream.eof()) 
		{	// end of function if position is outside the file
			cout << endl << "Position does not exist.";
			cout << endl << endl;
			return;
		};
		if (ch=='\n') 
		{	// if it's a '\n' then skip the two chracters and adjust the position
			Pos +=2; 
			i +=1;
		};
	};

	for (int i=Pos; i < (Pos+Amount);i++)
	{
		MyFstream.seekp(i);
		ch =MyFstream.get();
		if (MyFstream.eof())
		{	// if too many characters are to be replaced
			cout << endl << "Cannot overwrite enough characters.";
			cout << endl << endl;
			return;
		};
		if (ch == '\n')
		{	// same as above
			Amount +=2;
			i +=1;
			continue;
		};
			MyFstream.seekp(i);
			MyFstream << Overwrite;
	};
};


int main
I have had to figure this kind of thing out myself. I think (without having compiled and used your code) that it is one of those obnoxiousnesses of istreams.

The >> is a formatted input operator, which by default skips all whitespace. If you want to keep whitespace, you'll have to add
MyIfstream >> noskipws;

I've learned that dealing with iostreams has a lot of 'gotcha's. Hope this helps. :-)
Yes, it helped.

Thank you very much.

But how do I find such things out? Where do I find that?

int main
Topic archived. No new replies allowed.