473,809 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read binary File problem

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(sReadDat a, 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

Nov 5 '07 #1
4 3498

"Matrixinli ne" <an**********@g mail.comwrote in message
news:11******** *************@e 9g2000prf.googl egroups.com...
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(sReadDat a, 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
Nov 5 '07 #2
On 11 5 , 2 51 , Matrixinline <anup.kata...@g mail.comwrote:
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(sReadDat a, 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.


Nov 5 '07 #3
it does nothing with UNICODE, you will get the same error in ANSI
version.

Nov 5 '07 #4
On Nov 5, 7:51 am, Matrixinline <anup.kata...@g mail.comwrote:
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(sReadDat a, 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.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4615
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential options. I can get this to worksuccessfully, but only when I'm NOT dealing with arrays in thestructure definition. For example: In C, you are able to simply read a binary file intoa structure as follows: ...
3
18966
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
8
18670
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in adv.
1
2278
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
3
18462
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library functions of the language. But wondering the advantages of searching and reading from a binary file. If the files get too large, I guess the advantage of reading from a binary file is that it is much faster. Regards,
6
5815
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100 bytes of the file and some of these files are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the largest files and the other files take "forever" to get the last 100 bytes. This is the code I have currently that works with...
3
2964
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT datatype for the file instead of using IMAGE datatype. I can not use SqlDataReader to read the data. I need your help, Thanks. -David (1) I have a table TestFile for testing: ID int FileName navrchar(255)
9
3473
by: Matrixinline | last post by:
Hi All, Here is the problem char* memblock; std::ifstream file(sFileName, ios::in|ios::binary); if (file.is_open()) { size = file.tellg();
1
3933
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.