The tellg,seekg functions

Pages: 12
Hi.I need a function that displays the content of a text file on the console.
I've tried doing it like in the following code snippet,but it only works the first time I call it.The second time the program crashes(it gives an"invalid allocation size"error). Am I using the tellg and/or seekg functions incorrectly?

(a secondary question:char*bf seems to copy not only the content of the text file,but also some weird extra characters.How can I make it work properly?)
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
#include<iostream>
#include<fstream>
using namespace std;


 void display_text(fstream *pfile)
	{
		pfile->seekg(0,ios::end);
		int size=pfile->tellg();
		cout<<"\n size:"<<size;
		char*bf=new char[size];
		pfile->seekg(0);
		pfile->read(bf,size);
		cout.write(bf,size);
		cout<<endl;
		delete bf;
	}



int main()
{

fstream fisier;
fisier.open("ceva.txt",fstream::in|fstream::out);

fstream*ptof=&fisier;



cout<<"\n first attempt to display text:";
display_text(ptof);//first time it works


cout<<"\n second attempt:";
display_text(ptof);//second time it doesn't.me knows not why.me stupid



fisier.close();

return 0;
}
Last edited on
What is the output of your cout statements in the display_text() function?
The first cout is supposed to output the number of characters in "ceva.txt".I only put it in there to check if it does that correctly(it doesn't,it's always a bit larger than it should,hence the "weird characters"I was talking about).

The second cout displays all of the content in "ceva.txt" in the console.(plus the extra weird characters,of course).

Last edited on
Sorry, I was unclear: What is the size of the file and what is displayed for the size?
Hmmm.Ok,I rechecked my program.I got it wrong,the function calculates the number of characters corectly(I didn't consider tabs and new lines-oops!:D).However,the program still displays stuff that shouldn't be there.

Anyway,the first time I call the function,it gets the size correctly;the second time it says the size is -1,and the program crashes.
Anybody?
closed account (DSLq5Di1)
Line 16. delete[] bf;

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11

-edit-
Also those weird characters you mentioned are probably the uninitialized values of bf, replace line 11 with char*bf=new char[size]();.
Last edited on
I'm curious why you need to read a block of text and display it in this way. A more usual approach is open the file and read it line by line with getline.

If, as I suspect, you are running on Windows, your trailing characters are because the Windows end line sequences \r\n are being mapped to \n when you read the file, as you've opened the file in text mode. But your file size calculation is working on the actual size. So you're going to end up with one unused space at the end of buffer, after reading the file, for each end line.

To work round the problem either zero your buffer before use. That way, the unused trailing space will be filled with zeroes, so ending the string at the right point. Or used gcount to work out how many characters you actually read and terminate the buffer at the right place.

Andy

P.S. I would odify you code to pass the stream by reference rather than pointer. And if you're familar with vector, use vector<char> instead of new, etc.
Last edited on
@sloppy9: char*bf=new char[size](); this worked.I didn't know I could do that.thanks!

@andy: I changed the code and passed the variable by reference.I still get the same error.
I'm thinking it may have something to do with the seekg/tellg functions(as in I'm misusing them).

It's mind boggling that the same function call with the same variable works fine the first time and the second time not only does it not do what it's supposed to,but it also crashes the program.Isn't it?
I'm really curious about this.
closed account (DSLq5Di1)
Mmm did you make the change to line 16 also? could you please paste your current code. ^^
Can you run your program under a debugger?
@sloppy9: yes,I did change line 16;but even though it was wrong before,that particular mistake shouldn't have any effect on how the function works.it was just a memory leak.

@andy:I'm sorry,I don't understand your question.I thought that's what I'm doing whenever I run the program.I mean,that's how I know it doesn't work.Am I missing something?Sorry for my ignorance,I'm a total noob...
Usually debuggers can either "run" or "debug" an app. I was just checking that you were able to.

Do you get a clear stack trace when you crash?
@andy: What's a stack trace and how do I tell if it's clear or not?
What IDE are you using?
http://cplusplus.com/reference/iostream/istream/read/
If the End-of-File is reached before n characters have been read, (...) the failbit and eofbit will be set.

That means that the fstream will be in an invalid state, so the methods could fail (I think it is undefined).
Check out with gcount().

The crash happens because the size is -1, but new[] ask for and unsigned,
the conversion results in that you are trying to allocate 4GB
@andy:Visual C++;


An update: if I remove this: pfile->read(bf,size); from the function I don't get the error.So apparently this is what messes up the program,not seekg or tellg.
I still don't understand why,though.
I thought that it should throw an exception. ¿Is char*bf=new char[-1]; returning NULL?
¿What happens if you open the file in binary mode?
@ne55: I used gcount,and it's smaller then tellg.Is that the problem?I don't know how to solve this,I don't understand the flags stuff.I haven't read much about them.Am I supposed to use throw and catch()?Oh man,I have so much reading to do.Bleah!
Pages: 12