fread gives only the first 4 bytes ??

A

Axel

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);
 
K

Karl Heinz Buchegger

Axel said:
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?
 
A

Axel

Karl said:
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
 
K

Karl Heinz Buchegger

Axel said:
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.
 
?

=?iso-8859-1?Q?Andr=E9_P=F6nitz?=

Axel said:
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'
 
A

Axel

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... :)
 
P

Peter van Merkerk

Get yourself a hex editor (or write one :)
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.
 
P

Peter van Merkerk

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?
 
A

Axel

Peter said:
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...

Axel
 
P

Peter van Merkerk

After E0 there is 00...do you mean this with 0 Byte? But if it is
so,
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.
 
K

Karl Heinz Buchegger

Axel said:
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.
 
F

fa

Axel said:
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

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;

}
 
A

Axel

fa said:
CString is not in any standard c++ library, but is a MFC class.





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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top