unexpected results using file io

I have code similar to the following:
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
#include <iostream>
#include<windows.h>
#include<fstream>
#include<iomanip>
#include "kbd_stuff.h"

using namespace std;
int APIENTRY WinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpCmdLine,
                    int       nCmdShow)
{
	char output;	
	ifstream myfile; // we use ifstream to read from the file.
	myfile.open("typeme.txt", ios::in); // open the file for reading
	Sleep( 3000 );
	for( int m=0;m<100;m++)
	{
	while(!myfile.eof()) 
	{
		myfile.get(output); // this part does the reading--one char at a time.
		kcout(output);
		Sleep( 100 );
	}
	myfile.seekg(ios::beg); //get back to the beginning of the file
	ret();
	}
	myfile.close();
return 0;
}


with a header 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
#include <windows.h>

void kout(char * kp)
{

	int length=strlen(kp);
	
	for(int i=0;i<length;i++)
	{
		keybd_event(VkKeyScan(kp[i]),0,0,0); //depress
		keybd_event(VkKeyScan(kp[i]),0, KEYEVENTF_KEYUP,0); // release!

	
	}
	
		keybd_event(VK_RETURN,0x0D,0,0); //enter depress!
		keybd_event(VK_RETURN,0x0D, KEYEVENTF_KEYUP,0); // enter release!
	
		Sleep( 100 );
}


void kcout(char single) // we're going to call this function many times
						// and then a function to press [RETURN]
{
		keybd_event(VkKeyScan(single),0,0,0); //depress
		keybd_event(VkKeyScan(single),0, KEYEVENTF_KEYUP,0); // release!
}

void ret()
{
		keybd_event(VK_RETURN,0x0D,0,0); //enter depress!
		keybd_event(VK_RETURN,0x0D, KEYEVENTF_KEYUP,0); // enter release!
}

The output prints the text in the textfile named typeme.txt one time, adds a copy of the last character to the end, and keeps printing that character for the rest of the loop, how would I work around this?
I understand I can copy the entire text to a char array, and keep printing that over and over again, but I want to do it this way.. any ideas?
Last edited on
The stream's been marked as bad when it encountered the end of file. You need to change the state back to good.
how to do that?
myfile.setstate(std::ios::goodbit());
myfile.setstate(std::ios::goodbit);
Last edited on
Topic archived. No new replies allowed.