Trouble in file opening

Hello All,

I've been trying for to make a program in MS Visual C++ that will open a preexisting file in binary mode, and read a value from it. The project compiles, but fails to function. No matter what I've tried I can't seem to get it to open a file. Here is the code less one data structure (long!) and the include files for brevity's sake:
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
fstream GameStream;
char gamefile [MAX_PATH];
string TempString;
char *memblock;
ifstream::pos_type SaveSize;

int _tmain(int argc, _TCHAR* argv[])
{
cout << "Enter file name to open:";
cin >> gamefile;
//getline (cin, TempString); //guard for user input
//stringstream(TempString) >> gamefile; //same as prev line

//following comment was to confirm prog was looking in right directory

/* ofstream myfile;
myfile.open ("craptest.txt");
myfile << "Writing test line to file.\n";
myfile.close();
*/

GameStream.open (gamefile, ios::binary | ios::ate);
//GameStream.open ("save.gs0", ios::binary | ios::ate);
if( GameStream.is_open() )
		{
		cout << "File found, Opening....";
		SaveSize = (int) GameStream.tellg ();
		memblock = new char [SaveSize];
		GameStream.seekg (0, ios::beg);
		cout << "File Size is:" << SaveSize << "bytes\n";
		GameStream.read (memblock, SaveSize);
		GameStream.seekg (0, 68728);
		GameStream.read (Hero.CharName, 7);
		cout << "Hero.CharName" << Hero.CharName;
		return 0;
		}
else	{
		//code upon error opening file
		cout << "Error opening game save" << endl;
		return 0;
		}
return 0;
}


Any and all help greatly appreciated!
I suspect that what is happening is that you are inadvertently setting the eof bit on line 31. So *memblock actually did read fine. But since GameStream.eof() is now true, further I/O is not possible until you say:
GameStream.clear();

Hope this helps.
Thanks Duoas for reply, but not sure program is getting that far into the code. When I run debug, it automatically steps to the 'else' condition of the if statement on line 24. It seems as though the file can't be found, regardless of project directory I put it into. I've tried the debug directory etc, all to no avail. I've even deleted the file extension in case it was somehow interfering with my opening routine. Any other ideas?
Try setting gamefile to the full path of where you file is rather than just the file name. That way you'll know if the program can't find the file or if you have another problem like permissions.
Hey All, I got the file to work. Answer was problem with string not being opened and required the use of '.cstr()' to appended to end of 'gamefile' to allow it to find the file. Program works now and can read Hero.CharName correctly but now on to new issue. I'll post to a new thread to avoid mixing issues. :-) Thanks everyone for input.
Heh, that trips me up every time.
You confuse me, your solution above (about use of .cstr()) does not seem to apply to the code you have posted.
In the code you have posted gamefile is a char array not a string type so the code you posted (should) work.
Topic archived. No new replies allowed.