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?)
#include<iostream>
#include<fstream>
usingnamespace std;
void display_text(fstream *pfile)
{
pfile->seekg(0,ios::end);
int size=pfile->tellg();
cout<<"\n size:"<<size;
char*bf=newchar[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;
}
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).
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.
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.
@sloppy9: char*bf=newchar[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.
@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...
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.
@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!