Read binary File problem

M

Matrixinline

Hi All

Here is my problem I am using a Unicode project

and I tried to read the File like

sPath = LPCTSTR;

FILE* oFp = _tfopen(sPath,L"r");
while(!feof(oFp))
{

if (fgets(sReadData, 3072, oFp) != NULL)
{
sStringBuffer +=sReadData;
}
}
fclose(oFp);

But when I tried to Read a *.BMP file I get BM6 in the Buffer
sStringBuffer. after that It encounters end of file and closes down.

Can you please let me know where I am making a mistake to read files.

Thanks
Anup
 
Z

zhangyw80

Hi All

Here is my problem I am using a Unicode project

and I tried to read the File like

sPath = LPCTSTR;

FILE* oFp = _tfopen(sPath,L"r");
while(!feof(oFp))
{

if (fgets(sReadData, 3072, oFp) != NULL)
{
sStringBuffer +=sReadData;
}
}
fclose(oFp);

But when I tried to Read a *.BMP file I get BM6 in the Buffer
sStringBuffer. after that It encounters end of file and closes down.

Can you please let me know where I am making a mistake to read files.

Thanks
Anup
you should open file in binary mode using _tfopen(sPath,L"rb");
not _tfopen(sPath,L"r"); notice the 'b' in the second argument.
c runtime library may treat end-of-file of text and binary file in
different way.
 
C

Christopher Pisz

Hi All

Here is my problem I am using a Unicode project

and I tried to read the File like

sPath = LPCTSTR;

FILE* oFp = _tfopen(sPath,L"r");
while(!feof(oFp))
{

if (fgets(sReadData, 3072, oFp) != NULL)
{
sStringBuffer +=sReadData;
}
}
fclose(oFp);

But when I tried to Read a *.BMP file I get BM6 in the Buffer
sStringBuffer. after that It encounters end of file and closes down.

Can you please let me know where I am making a mistake to read files.

Thanks
Anup



You are using non-standard function calls and types, no its anyone's guess
without seeing what those are.
For example, What is this _tfopen
 
J

James Kanze

Here is my problem I am using a Unicode project
and I tried to read the File like
sPath = LPCTSTR;
FILE* oFp = _tfopen(sPath,L"r");
while(!feof(oFp))
{
if (fgets(sReadData, 3072, oFp) != NULL)
{
sStringBuffer +=sReadData;
}
}
fclose(oFp);

This doesn't look like C++ to me, although there are some legacy
C++ functions involved. Still, if _tfopen returns a C
compatible FILE* (which would seem to be the case if you use
feof, fgets and fclose on it), then I can see some real
problems: I'm not familiar with the .BMP format, but if it's
binary (or really any exterally defined format), you should open
in binary mode. And fgets (the C equivalent of getline) reads
lines of text, not binary data.
But when I tried to Read a *.BMP file I get BM6 in the
Buffer sStringBuffer. after that It encounters end of file and
closes down.

Just a guess, but if you're under Windows, reading a file in
text mode, you'll see an EOF the first time you encounter a byte
with 0x1A.
Can you please let me know where I am making a mistake to read
files.

Without knowing really what kind of data you are trying to read,
it's difficult to say. If it's text data, try forgoing the
non-standard stuff, and just use std::istream. If it's binary,
the question is a bit more complex; depending on the
application, std::istream or std::streambuf might be the
solution, or it might be preferable to go directly to the system
level.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top