473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

fread gives only the first 4 bytes ??

Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.
I got this method from a tutorial and though it works...what's wrong? Is
the buffersize for the String 'FileContent' not ok? Before fread the
size of 'FileContent' is correct, but after the operation its truncated
to 4.

The Codepart
--------------------------------------

int fsize;
CString FileContent;
LPTSTR pStr;
FILE *fl = fopen(ff,"rb");
if(!fl)
{
//Error Message
}
else
{
fseek(fl, 0, SEEK_END);
fsize = ftell(fl);
pStr = FileContent.GetBufferSetLength((int)fsize + 1);
fseek(fl, 0, SEEK_SET);
c_edit1.SetWindowText(itoa(fsize, buf, 100));
fsize = fread(pStr, 1, fsize, fl);
*(pStr + fsize) = '\0';
FileContent.ReleaseBuffer(-1);
}
fclose(fl);

--------------------------------------

Thanks for your help!

Axel

Jul 19 '05 #1
12 6313


Axel wrote:

Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.


What's in that binary data?
Could it be that there is a binary 0 byte in it?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2
Karl Heinz Buchegger wrote:

Axel wrote:
Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.

What's in that binary data?
Could it be that there is a binary 0 byte in it?


It does not matter what file i use..I always get a 4 byte result. But
here is an example file (the first chars only):

II*   þ     á   ó   Â     

Looks funny...here is another file:

ÿØÿà JFIF  d d ÿì Ducky   P ÿî Adobe dÀ ÿÛ „ 

Maybe the binary 0 byte is the problem..i dont know.

Axel

Jul 19 '05 #3


Axel wrote:

Karl Heinz Buchegger wrote:

Axel wrote:
Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.

What's in that binary data?
Could it be that there is a binary 0 byte in it?


It does not matter what file i use..I always get a 4 byte result. But
here is an example file (the first chars only):

II*   þ     á   ó   Â     

Looks funny...here is another file:

ÿØÿà JFIF  d d ÿì Ducky   P ÿî Adobe dÀ ÿÛ „ 

Maybe the binary 0 byte is the problem..i dont know.


Get yourself a hex editor (or write one :-)
This tools is an absolute *must* when working with binary files.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
Axel <ax************@tecart.de> wrote:
Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.
I got this method from a tutorial and though it works...what's wrong? Is
the buffersize for the String 'FileContent' not ok? Before fread the
size of 'FileContent' is correct, but after the operation its truncated
to 4.

The Codepart
--------------------------------------

int fsize;
CString FileContent;
LPTSTR pStr;
FILE *fl = fopen(ff,"rb");
if(!fl)
{
//Error Message
}
else
{
fseek(fl, 0, SEEK_END);
fsize = ftell(fl);
pStr = FileContent.GetBufferSetLength((int)fsize + 1);
fseek(fl, 0, SEEK_SET);
c_edit1.SetWindowText(itoa(fsize, buf, 100));
fsize = fread(pStr, 1, fsize, fl);
*(pStr + fsize) = '\0';
FileContent.ReleaseBuffer(-1);
}
fclose(fl);

--------------------------------------


A C++ solution might look similar to

#include <iostream>
#include <fstream>
#include <ios>
#include <iterator>
#include <string>

using namespace std;

int main()
{
ifstream is("filename");
if (!is) {
// Error
}
else {
is.unsetf(ios::skipws);
string str((istream_iterator<char>(is)), istream_iterator<char>());
cout << str << endl;
}
}

Untested, though.

Andre'
Jul 19 '05 #5
Karl Heinz Buchegger wrote:

Get yourself a hex editor (or write one :-)
This tools is an absolute *must* when working with binary files.


Ok..true words. :-) Here is the second file in HEX:

FF D8 FF E0 00 10 4A 46 49 46 00 01 02 00 00 64

After E0 there is 00...do you mean this with 0 Byte? But if it is so,
how can I write the binary data into a string type. (I would use type
char*, but how I write 10MB into a char* ?) Problems problems... :-)

Jul 19 '05 #6
> > Get yourself a hex editor (or write one :-)
This tools is an absolute *must* when working with binary files.


Ok..true words. :-) Here is the second file in HEX:

FF D8 FF E0 00 10 4A 46 49 46 00 01 02 00 00 64

After E0 there is 00...do you mean this with 0 Byte? But if it is so,
how can I write the binary data into a string type. (I would use type
char*, but how I write 10MB into a char* ?) Problems problems... :-)


Well assuming you want to display binary data as ASCII, you could
replace the 0 bytes (and probably also any other byte < 32) with an
ASCII character that represents non-displayable data. Hex editors
usually use . for non-displayable bytes.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #7
>
After E0 there is 00...do you mean this with 0 Byte? But if it is so,
how can I write the binary data into a string type. (I would use type
char*, but how I write 10MB into a char* ?) Problems problems... :-)


Why do you want to put binary data in a string type anyway?

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #8
Peter van Merkerk wrote:
After E0 there is 00...do you mean this with 0 Byte? But if it is so,
how can I write the binary data into a string type. (I would use type
char*, but how I write 10MB into a char* ?) Problems problems... :-)

Why do you want to put binary data in a string type anyway?

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


I want to encode it Base64..for a little personal EMail tool using CDO.
So I read the Attachments into a string, encode it and can put them into
the mailsource. So far my theory...

Axel

Jul 19 '05 #9
> >>After E0 there is 00...do you mean this with 0 Byte? But if it is
so,
how can I write the binary data into a string type. (I would use typechar*, but how I write 10MB into a char* ?) Problems problems... :-)
Why do you want to put binary data in a string type anyway?


I want to encode it Base64..for a little personal EMail tool using

CDO. So I read the Attachments into a string, encode it and can put them into the mailsource. So far my theory...


Why do you need to put the binary data it into a string first?
You could copy the binary data into a std::string like this:

fsize = fread(pStr, 1, fsize, fl);
std::string s (pStr, fsize);

std::string doesn't require the string to be zero terminated, i.e. 0
bytes are allowed within the string.

But I don't see the advantage of copying the binary bytes to the string.
Why not three read bytes directly from pStr at a time and encode them to
four Base64 encode characters and add those characters to the string.
Since you can calculate how long the Base64 encode string will become in
advance, it is advisable to call std::string::reserve() with the length
of the Base64 encoded string before actually filling the string.

Also note that there are libraries that can do the Base64 encoding for
you.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 19 '05 #10


Axel wrote:

Karl Heinz Buchegger wrote:
Get yourself a hex editor (or write one :-)
This tools is an absolute *must* when working with binary files.

Ok..true words. :-) Here is the second file in HEX:

FF D8 FF E0 00 10 4A 46 49 46 00 01 02 00 00 64

After E0 there is 00...do you mean this with 0 Byte?


Yep.
But if it is so,
how can I write the binary data into a string type.


By using a string type which doesn't care about the 0 character
in it. Eg. std::string doesn't. MFC's CString seems to care.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #11
fa
Axel <ax************@tecart.de> wrote in message news:<zE**********************@news.easynews.com>. ..
Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.
I got this method from a tutorial and though it works...what's wrong? Is
the buffersize for the String 'FileContent' not ok? Before fread the
size of 'FileContent' is correct, but after the operation its truncated
to 4.

The Codepart
--------------------------------------

int fsize;
CString FileContent;
CString is not in any standard c++ library, but is a MFC class.

LPTSTR pStr;
FILE *fl = fopen(ff,"rb");
for dealing with files you can use streams in c++. FILE was introduced
in c.

try:

#include<string>
#include<fstream>
#include<iterator>

using namespace std;

int
main()
{

ifstream file; // it's a stream on input files
string buf; //it's a character string

file.open("the_name_of_the_file", ios::binary);
if(!file)
{
something_here();
}

else
{
copy(istream_iterator<char>(file), istream_iterator<char>(),
back_inserter(buf));
/*sould copy the whole content of the file. I'm not sure if will be
added an end of file to buf. */
}

return 0;

}

if(!fl)
{
//Error Message
}
else
{
fseek(fl, 0, SEEK_END);
fsize = ftell(fl);
pStr = FileContent.GetBufferSetLength((int)fsize + 1);
fseek(fl, 0, SEEK_SET);
c_edit1.SetWindowText(itoa(fsize, buf, 100));
fsize = fread(pStr, 1, fsize, fl);
*(pStr + fsize) = '\0';
FileContent.ReleaseBuffer(-1);
}
fclose(fl);

--------------------------------------

Thanks for your help!

Axel

Jul 19 '05 #12
fa wrote:


CString is not in any standard c++ library, but is a MFC class.
LPTSTR pStr;
FILE *fl = fopen(ff,"rb");

for dealing with files you can use streams in c++. FILE was introduced
in c.

try:

#include<string>
#include<fstream>
#include<iterator>

using namespace std;

int
main()
{

ifstream file; // it's a stream on input files
string buf; //it's a character string

file.open("the_name_of_the_file", ios::binary);
if(!file)
{
something_here();
}

else
{
copy(istream_iterator<char>(file), istream_iterator<char>(),
back_inserter(buf));
/*sould copy the whole content of the file. I'm not sure if will be
added an end of file to buf. */
}

return 0;

}

Thanks for all helpers! The above version works fine. Now i run into an
other problem, but I will open a new thread for this. :-)

Axel

Jul 19 '05 #13

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

Similar topics

2
by: RootShell | last post by:
Dear All Im having some dificulty here: I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair Spectrum $SCREEN Image Format) and tranforms it into PNG format to show onscreen...
5
by: syntax | last post by:
hi here is my test code include<stdio.h> #include<stdlib.h> int main() {
22
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
2
by: Fernando Barsoba | last post by:
Hi all, I'm trying to read from a binary file, and I'm using 'fread()' as indicated in function 'getfile()'. The variable 'bytesread' shows that I have read the 557,000 bytes from the file, but...
6
by: Claude Yih | last post by:
Hi, everyone. I noticed an interesting thing about fread() this afternoon. Well, I can't see why so I post this message in the hope of getting some explanation. Please help me. I wrote the...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
3
by: juleigha27 | last post by:
Hi, First off, I want to apologize if this already got posted it seemed like something happened when I tried to post it previously and it didn't work. I am new to file manipulation with c. I...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.