fread gives only the first 4 bytes ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Axel

    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.Get BufferSetLength ((int)fsize + 1);
    fseek(fl, 0, SEEK_SET);
    c_edit1.SetWind owText(itoa(fsi ze, buf, 100));
    fsize = fread(pStr, 1, fsize, fl);
    *(pStr + fsize) = '\0';
    FileContent.Rel easeBuffer(-1);
    }
    fclose(fl);

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

    Thanks for your help!

    Axel

  • Karl Heinz Buchegger

    #2
    Re: fread gives only the first 4 bytes ??



    Axel wrote:[color=blue]
    >
    > 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.[/color]

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

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Axel

      #3
      Re: fread gives only the first 4 bytes ??

      Karl Heinz Buchegger wrote:[color=blue]
      >
      > Axel wrote:
      >[color=green]
      >>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.[/color]
      >
      >
      > What's in that binary data?
      > Could it be that there is a binary 0 byte in it?
      >[/color]

      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

      Comment

      • Karl Heinz Buchegger

        #4
        Re: fread gives only the first 4 bytes ??



        Axel wrote:[color=blue]
        >
        > Karl Heinz Buchegger wrote:[color=green]
        > >
        > > Axel wrote:
        > >[color=darkred]
        > >>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.[/color]
        > >
        > >
        > > What's in that binary data?
        > > Could it be that there is a binary 0 byte in it?
        > >[/color]
        >
        > 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.[/color]

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

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • André Pönitz

          #5
          Re: fread gives only the first 4 bytes ??

          Axel <axel.friedrich @tecart.de> wrote:[color=blue]
          > 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.Get BufferSetLength ((int)fsize + 1);
          > fseek(fl, 0, SEEK_SET);
          > c_edit1.SetWind owText(itoa(fsi ze, buf, 100));
          > fsize = fread(pStr, 1, fsize, fl);
          > *(pStr + fsize) = '\0';
          > FileContent.Rel easeBuffer(-1);
          > }
          > fclose(fl);
          >
          > --------------------------------------[/color]

          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_it erator<char>(is )), istream_iterato r<char>());
          cout << str << endl;
          }
          }

          Untested, though.

          Andre'

          Comment

          • Axel

            #6
            Re: fread gives only the first 4 bytes ??

            Karl Heinz Buchegger wrote:

            [color=blue]
            > Get yourself a hex editor (or write one :-)
            > This tools is an absolute *must* when working with binary files.
            >[/color]

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



            Comment

            • Peter van Merkerk

              #7
              Re: fread gives only the first 4 bytes ??

              > > Get yourself a hex editor (or write one :-)[color=blue][color=green]
              > > This tools is an absolute *must* when working with binary files.
              > >[/color]
              >
              > 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... :-)[/color]

              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.merke rk(at)dse.nl


              Comment

              • Peter van Merkerk

                #8
                Re: fread gives only the first 4 bytes ??

                >[color=blue]
                > 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... :-)[/color]

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

                --
                Peter van Merkerk
                peter.van.merke rk(at)dse.nl


                Comment

                • Axel

                  #9
                  Re: fread gives only the first 4 bytes ??

                  Peter van Merkerk wrote:
                  [color=blue][color=green]
                  >>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... :-)[/color]
                  >
                  >
                  > Why do you want to put binary data in a string type anyway?
                  >
                  > --
                  > Peter van Merkerk
                  > peter.van.merke rk(at)dse.nl
                  >
                  >[/color]

                  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

                  Comment

                  • Peter van Merkerk

                    #10
                    Re: fread gives only the first 4 bytes ??

                    > >>After E0 there is 00...do you mean this with 0 Byte? But if it is
                    so,[color=blue][color=green][color=darkred]
                    > >>how can I write the binary data into a string type. (I would use[/color][/color][/color]
                    type[color=blue][color=green][color=darkred]
                    > >>char*, but how I write 10MB into a char* ?) Problems problems... :-)[/color]
                    > >
                    > > Why do you want to put binary data in a string type anyway?[/color]
                    >
                    > I want to encode it Base64..for a little personal EMail tool using[/color]
                    CDO.[color=blue]
                    > So I read the Attachments into a string, encode it and can put them[/color]
                    into[color=blue]
                    > the mailsource. So far my theory...[/color]

                    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::re serve() 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.merke rk(at)dse.nl








                    Comment

                    • Karl Heinz Buchegger

                      #11
                      Re: fread gives only the first 4 bytes ??



                      Axel wrote:[color=blue]
                      >
                      > Karl Heinz Buchegger wrote:
                      >[color=green]
                      > > Get yourself a hex editor (or write one :-)
                      > > This tools is an absolute *must* when working with binary files.
                      > >[/color]
                      >
                      > 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?[/color]

                      Yep.
                      [color=blue]
                      > But if it is so,
                      > how can I write the binary data into a string type.[/color]

                      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
                      kbuchegg@gascad .at

                      Comment

                      • fa

                        #12
                        Re: fread gives only the first 4 bytes ??

                        Axel <axel.friedrich @tecart.de> wrote in message news:<zE7mb.669 4142$cI2.953392 @news.easynews. com>...[color=blue]
                        > 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;[/color]

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

                        [color=blue]
                        > LPTSTR pStr;
                        > FILE *fl = fopen(ff,"rb");[/color]

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

                        try:

                        #include<string >
                        #include<fstrea m>
                        #include<iterat or>

                        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_fil e", ios::binary);
                        if(!file)
                        {
                        something_here( );
                        }

                        else
                        {
                        copy(istream_it erator<char>(fi le), istream_iterato r<char>(),
                        back_inserter(b uf));
                        /*sould copy the whole content of the file. I'm not sure if will be
                        added an end of file to buf. */
                        }

                        return 0;

                        }

                        [color=blue]
                        > if(!fl)
                        > {
                        > //Error Message
                        > }
                        > else
                        > {
                        > fseek(fl, 0, SEEK_END);
                        > fsize = ftell(fl);
                        > pStr = FileContent.Get BufferSetLength ((int)fsize + 1);
                        > fseek(fl, 0, SEEK_SET);
                        > c_edit1.SetWind owText(itoa(fsi ze, buf, 100));
                        > fsize = fread(pStr, 1, fsize, fl);
                        > *(pStr + fsize) = '\0';
                        > FileContent.Rel easeBuffer(-1);
                        > }
                        > fclose(fl);
                        >
                        > --------------------------------------
                        >
                        > Thanks for your help!
                        >
                        > Axel[/color]

                        Comment

                        • Axel

                          #13
                          Re: fread gives only the first 4 bytes ??

                          fa wrote:
                          [color=blue]
                          >
                          >
                          > CString is not in any standard c++ library, but is a MFC class.
                          >
                          >
                          >[color=green]
                          >>LPTSTR pStr;
                          >>FILE *fl = fopen(ff,"rb");[/color]
                          >
                          >
                          > for dealing with files you can use streams in c++. FILE was introduced
                          > in c.
                          >
                          > try:
                          >
                          > #include<string >
                          > #include<fstrea m>
                          > #include<iterat or>
                          >
                          > 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_fil e", ios::binary);
                          > if(!file)
                          > {
                          > something_here( );
                          > }
                          >
                          > else
                          > {
                          > copy(istream_it erator<char>(fi le), istream_iterato r<char>(),
                          > back_inserter(b uf));
                          > /*sould copy the whole content of the file. I'm not sure if will be
                          > added an end of file to buf. */
                          > }
                          >
                          > return 0;
                          >
                          > }
                          >
                          >
                          >[/color]


                          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

                          Comment

                          Working...