Re: Failed to read a file
"Matrixinline" <anup.kataria@gmail.com> wrote in message
news:1193993009.857917.162580@i38g2000prf.googlegroups.com...
Hi All,
Here is the problem
char* memblock;
std::ifstream file(sFileName, ios::in|ios::binary);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the complete file content is in memory";
delete[] memblock;
}
I tried to read a say "JPEG/GIF/EXE/etc.. " files.
I checked in to debugger and find that It reads only
BM? from file it means that it does not read till the end of file can
you please let me know why this problem I am facing
One more thing My project is a Unicode one I hope this one will not
affect the File I/O functions
Thanks
Anup
===========
You open the file, get the position. The position at this point is 0. Did
you mean to add the line
file.seekg (0, std::ios::end);
before the size = file.tellg();?
It is hard to say, because this code doesn't compile as written for me.
This, compilable version, runs for me and shows the output of a txt file (I
don't care to look at binary data at this time).
#include <iostream>
#include <string>
#include <fstream>
int main()
{
char* memblock;
std::string sFileName="console5.cpp";
std::ifstream file(sFileName.c_str(), std::ios::in|std::ios::binary);
if (file.is_open())
{
file.seekg (0, std::ios::end);
unsigned int size = file.tellg();
std::cout << "Size:" << size << "\n";
memblock = new char [size];
file.seekg (0, std::ios::beg);
file.read (memblock, size);
file.close();
std::cout << "the complete file content is in memory\n";
for ( int i = 0; i < size; ++i )
std::cout << memblock[i];
delete[] memblock;
}
}