Re: [Dev-C++] file.read() dosn't read all it should read
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Gerson L. <ja...@ya...> - 2005-02-18 17:04:54
|
Hi all!
Iv'e seen you code, and it's almost axactly like mine!
That has always worked for me, so, I will just show you the little differences, maybe one of them brings your code to life!
int main(int argc, char* argv[]) // classic
{
//just printing the parameter list
cout << "running " << argv[0] << "\nwith parameters:\n";
for(int i = 1; i < argc; i++) cout << argv[i] << " ";
if(argc == 2){
cout << "\nTrying to open file " << argv[1] << endl;
ifstream BMP(argv[1], ios::in);
long FileSize = 0;
if(!BMP){
cout << "Could not open file\n";
return 0;
}else{
cout << "File opened\n";
BMP.seekg(0,ios::end); FileSize = BMP.tellg(); BMP.seekg(0, ios::beg);
p_buffer = new char[FileSize];// careful if you do it this way!
// supose p_buffer is of type char*
cout << "File size: " << FileSize << endl;
BMP.read( p_buffer, FileSize);
////// the rest of code goes here
Now: the differences:
You didn't show how you opened the file, so, may be there a "little thing"
I open it like this:
ifstream BMP(argv[1], ios::in);
or:
ifstream BMP("fichero.txt", ios::in);
the important for me here is: I use ifstream if Im gonna read the file, ofstream for writing. ( fstream ONLY if i need to read/write to the same file in a single operation)
the modes tha I use are: ios::in, ios::out, ios::app, ios::trunc, ios::bin
Then, the way of retrieving the file size is tha same, except for I use only 'ios' instead of ios_'base'.
I don't think that to be so important, but, exist tha posibility that there is some inheritance theme here, using just ios::whatever has allways worked fine for me.
The rest of thing seem to be just fine!!
So , I can only say tha the trouble seems to be with the opening of the file.
In my few expreriences, if you read a file to the end, I mean, if you read "all the file" once, you'll not be able to read again, even moving the read pointer ( i.e. seekg(0, ios::beg ), unless you close the file and re-open it. Is that happening to you? I recomend to read all the file, and close it, so if you need it later, you have it in RAM, you can modify the contens of the file in RAM and then use ofstream to replace the contens of the file to actualize it.
Im a very satisfied user of fstream, i can even read binary files with it, like bitmap files or .mp3 files. so I can tell fstream is powerful. So if you can't resolve, tell me more about your code.
Notes: I use Dev C++ 4.1
Question: what are you doing with this? ::: ap_socket->send(p_buffer, file_size);
is it networking stuff?
Jakob EKlund <mai...@ko...> wrote:
Ok, here is the thing.
I'm trying to read from a file. But for some reason the entire file wont
read.
It's really strange becaus fstream::read() is suposed to read all the data
at once (unlike readsome() )
Has any one of you ever run in to this problem.
The source:
//get the filesize
file.seekg( 0,ios_base::end );
file_size = file.tellg();
file.seekg( 0,ios_base::beg );
p_buffer = new char[file_size];
cout << "file size:" << file_size<
file.read(p_buffer, file_size);
cout << "gcount: "<< file.gcount()<
ap_socket->send(p_buffer, file_size);
delete p_buffer;
the output:
file size:6090
gcount: 5
---------------------------------
|