Problems reading text files [True!]

Hi all,

My program has to read out a configuration file. This file contains some list with servers.
This works fine. The second part is finding the oldest log file on a specified path on the server. (For every server again). This also works fine.

I end up with the file path. I have to read out the whole file. Now comes the problem. Whatever I try, i only get back:

(SPACE)(some block, white)B

The file is a .log file. All permissions are fine. Readable by everyone. I can also read it via notepad.

To be clear:
Windows machine
Windows network
Reading file OVER the network into the c$ share.
The file is a text file.
I get bogus data back.
The right size is returned when seeking.
I am using C++ with fstream

My 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
38
	fstream fpRes, fpLog;
	string logFile;
	logFile.clear();
	uint32_t fileLen;

	// Open the result file
	fpRes.open(fileName.c_str(), fstream::binary | fstream::app | fstream::out);
	if(!fpRes.is_open())
		return -3;

	/// Find newest file
	logFile = this->findNewestFile(server->path);
	if(!logFile.length())
		return -4;

	// Open the log file
	fpLog.open(logFile.c_str(),fstream::in);
	if(!fpLog.is_open())
		return -3;

	// Find the file size
	fpLog.seekg(0, ios_base::end);
	fileLen = fpLog.tellg();
	fpLog.seekg(0);

	char *buf = new char[fileLen];
	if(!buf)
		return -2;

	fpLog.read(buf,fileLen);
	buf[fileLen-1] = '\0';

cout << "FileLength: " << fileLen << endl;
cout << "Data: " << endl << buf << endl;

	fpLog.close();
	fpRes.close();


Thanks!

// Jarvix
I've found the STL does that kind of bogus bunkus with files when you don't open in ios::binary mode.
    22         fpLog.open(logFile.c_str(),ios::in|ios::binary);

If that doesn't help, then try this also: instead of using fpLog.seekg(0);, just close the file and open it again.

Alas.
Thanx for the suggestions, but both solutions did not solve the problem.
They made no changes to the result.

I made a textfile in the same directory I want to read a file from. Succeeds.
I renamed the file to something with .log (test.log) and put the same data in
it as in the file I want to read for real. Succeeds.

I really don't understand why it fails reading the files.

Thanks in advance,

Jarvix

I don't either. Did you check the fstream state before and after attempting to read()?

BTW, you've got an off-by-one with fileLen. You need fileLen + 1 chars to store the data plus a terminating null.

Also, fileLen should be declared as type streamsize.
Did you mean binary only? I tested with in and binary.
I set it to binary only, and now i get a failed opening file. fpLog.flags() is 4098.

good() is 0, fail() is 1, any possibility to get exacter errors?

I changed what you mentioned above.

Jarvix
Last edited on
Anyone?

May it me a networking problem?
Topic archived. No new replies allowed.