ifstream

Hello, everyone, thanks for the site and the support.
I need to read an ansi encoded .txt file.
(http://www.cplusplus.com/reference/iostream/istream/read/)

I'am creating a class that has char* fileData that is initialized to null on constructor and delete[] if not null in destructor.

I use fileData as follows:

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
char* FileLogger::read(const std::string& fileName)
{
	std::ifstream myInputFileStream;
	try
	{
		long fileLength;
	
		myInputFileStream.open( fileName.c_str());
		if(!myInputFileStream)
		{
			throw std::exception("Error opening the requested file.");
		}
		
		myInputFileStream.seekg (0, ios::end);
		fileLength = myInputFileStream.tellg();

		fileData = new char[fileLength];

		myInputFileStream.seekg (0, ios::beg);

		myInputFileStream.read(fileData, fileLength);
		
		myInputFileStream.close();

		return fileData;
		

	}
	catch(std::exception& myExc)
	{
		if(myInputFileStream.is_open())
		{
			myInputFileStream.close();
		}

		std::cout << myExc.what() << endl;
	}

}


then on my main:

 
printf("%s", myFL.read("C:\\Documents and Settings\\Neo_Geo\\Desktop\\blues.txt"));


but it adds some extra weird chars at the end.
Could someone help me out please?
Thank you in advance.
The printf call expects the string to be null-terminated.

Try:
1
2
3
fileData = new char[fileLength + 1];
//...
fileData[fileLength] = 0;
Last edited on
I tried what you said and

fileData[fileLength] = '\0';

but still the same :(
Last edited on
I'm stumped then. Could it be something with the seekg/tellg to get the length?
In the console I get:

1
2
3
4
5
chord movement of F7-Bb7-F7-C7.
You'll hear similar eight-bar chord progressions on songs like
"Someday After a While" by Freddie King,
"It Hurts Me Too" by Elmore James and
"Sitting on Top of the World" by Howlin' Wolf.════


The .txt ends at "...Wolf."
I don't have any trailing cr/lf or white spaces and it is saved in Ansi encoding.
Anyways, I'll check back tomorrow, goodnight everyone.
Last edited on
To be sure - what you can do is set the buffer array to 0 to begin with.
fileData = new char[fileLength+1](); //the () at the end means initialise to zero
This works guestgulkan, thank you! (I will mark as solved in the night...)

What is the cost of initializing this way all reserved memory address to 0 pointed to by fileData?

Why can't I just initialize the last element?

Oh and should I init to length+1? for the extra null?
Because it works for both

1
2
fileData = new char[fileLength + 1]();
fileData = new char[fileLength]();


Am I waisting a byte in the 1st one?
Last edited on
I really don't get it...even

fileData = new char[fileLength -3]();

works just fine. What's happening?
Instead of using C-strings and stuff, why can't you just use an std::string?
firedraco...,but ifstream member method .read() takes char* to write to, what can I do?

By the way if I do something like

1
2
3
4
5
string s1;
string s2="contents of 100mb txt file";
s1.assign(s2);
s2.clear();
s1.clear();


Is the memory reclaimed?
If I don't do .clear(), does it happen when s1, s2 go out of scope or not?
you should open the file in binary mode myInputFileStream.open( fileName.c_str(), ios::binary);

remember that newline in windows is CR+LF (that's 2 bytes), if you are not reading the file in binary mode fstream is interpreting the CR+LF as only one character (2bytes read as one)..

hope that helps
Opening in ios::binary

with

1
2
fileData = new char[fileLength+1];
fileData[fileLength] = '\0';


works fine.
So just to make sure I understand,
everytime I need to reserve the size of the file in bytes + 1 to store the null terminator.

Thank you very much guys, blackcoder41 that makes sense about Cr\Lf thank you for pointing that out!
Topic archived. No new replies allowed.