ofstream printing

Hi, I've run into the following problem, and I just can't figure out why the program does what it does.
My code looks just like the code below, and inside the write function i've tried
both option 1, and 2. Option 1 works fine, it will print every letter on a separate line. But option 2 doesnt print anything. WHY??? This doesnt make sense to me!

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
void write (char ch, ofstream &out);

int main() 
{
    //some stuff
    write(i, fout);
}

void write (char ch, ofstream &out)
{
  out << ch << std::endl; //option 1
  out << ch; //option 2
}


Unless you really mess something up in //some stuff this should not happen.
Show full code which fails to write characters.
This prints strangely, i'll give examples

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
#include <iostream>
#include <Windows.h>
#include <fstream>


void write(char ch, std::ofstream &out);

int main()
{
	std::cout << "Program Start..." << std::endl;
	std::ofstream fout("output.txt");
	fout << "Keys: \n" << std::endl;
	
	char i;
	while(true)
	{
		for(i = 8; i <= 190; i++)
		{
			if(GetAsyncKeyState(i) & 0x0001)
			{
				write(i,fout);
				fout << "Hello" << std::endl;
			}
		}
	}
	
	
	return 0;
}

void write(char ch, std::ofstream &out)
{
	out << ch << std::endl;
	out << ch;
}

If I run the code, and type "one(space)two(enter)three" it will look like this:
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
Keys: 

O
OHello
N
NHello
E
EHello
 
 Hello
T
THello
W
WHello
O
OHello



Hello
T
THello
H
HHello
R
RHello
E
EHello
E
EHello

Hello
however if I get rid of the std::endl; nothing will appear: this program prints "Keys: " and that is all
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
#include <iostream>
#include <Windows.h>
#include <fstream>


void write(char ch, std::ofstream &out);

int main()
{
	std::cout << "This program is legit" << std::endl;
	std::ofstream fout("output.txt");
	fout << "Keys: \n" << std::endl;
	
	char i;
	while(true)
	{
		for(i = 8; i <= 190; i++)
		{
			if(GetAsyncKeyState(i) & 0x0001)
			{
				write(i,fout);
				fout << "Hello";
			}
		}
	}
	
	
	return 0;
}

void write(char ch, std::ofstream &out)
{
	out << ch;
}

And what seems strange? Everything is expected
Your code prints character, newline symbol, character again, Hello line and a newline, so it looks like:
1
2
3
//<Character
█Hello
//<carriage is here 


It repeats it in a loop.

Edit: How do you terminate your program? If you forcebly stop it file is not guaranteed to be written on disc.
Last edited on
My question is why the code in the second response doesn't print anything.

How would I go about terminating the program?
My question is why the code in the second response doesn't print anything.
Because program never fineshes properly, fstream destuctor is never called and its buffer is never flushed.

How would I go about terminating the program?

Like you want.
For example you can catch press of some specific key (escape for example) and break from the outer loop.
Topic archived. No new replies allowed.