binary file to CString

Discussion in 'C++' started by Carl Forsman, Nov 19, 2008.

  1. Carl Forsman

    Carl Forsman Guest

    given the following base64_encode function, I will read bytes from a
    file and encode the binary data into a single line CString;

    but something wrong with the following code with errors!

    ================
    std::string base64_encode(unsigned char const* bytes_to_encode,
    unsigned int in_len) {

    ================
    #define BYTE unsigned char
    ....
    int size;
    BYTE* buffer;
    ....
    std::ifstream inputFile("C:\\test.bin", std::ios::in |
    std::ios::binary);

    // open the binary file to read
    if (inputFile.is_open())
    {

    inputFile.seekg(0, std::ios::end);
    size = inputFile.tellg(); // size of the file

    buffer = new BYTE[size];
    inputFile.read(buffer, size);

    // encode binary data into a Base64 string
    std::string data = base64_encode(buffer, size);
    // convert std::string to CString
    CString st = CString(data);

    }
     
    Carl Forsman, Nov 19, 2008
    #1
    1. Advertising

  2. Carl Forsman a écrit :
    > given the following base64_encode function, I will read bytes from a
    > file and encode the binary data into a single line CString;
    >
    > but something wrong with the following code with errors!

    [snip]
    > inputFile.seekg(0, std::ios::end);
    > size = inputFile.tellg(); // size of the file
    >
    > buffer = new BYTE[size];


    > inputFile.read(buffer, size);


    Your file cursor is at the end of the file. Add the following before
    reading:
    inputFile.seekg(0,ios::beg);
    --
    Michael
     
    Michael DOUBEZ, Nov 19, 2008
    #2
    1. Advertising

  3. Carl Forsman

    Stefan Ram Guest

    Carl Forsman <> writes:
    >inputFile.seekg(0, std::ios::end);


    In ISO/IEC 9899:1990:

    »A binary stream need not meaningfully support
    fseek calls with a whence value of SEEK_END.«

    Footnote 225 even mentions »undefined behavior«:

    »225) Setting the file position indicator to end-of-file,
    as with fseek(file, 0, SEEK_END), has undefined behavior
    for a binary stream (because of possible trailing null
    characters) or for any stream with state-dependent
    encoding that does not assuredly end in the initial shift
    state.«

    In ISO/IEC 14882:2003(E), »seekg« and »tellg« seem to be
    defined using »pubseekoff«, which seems to be based on
    »seekoff«, which seems to be based on »::std::fseek«,
    for which the above quotation from ISO/IEC 9899:1990 applies.
     
    Stefan Ram, Nov 19, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    4,611
  2. yeye yaya

    I hate cstring!!!

    yeye yaya, Jul 11, 2003, in forum: C++
    Replies:
    7
    Views:
    2,750
    Stuart Golodetz
    Jul 11, 2003
  3. Rajesh Garg

    Why is CString not preferred

    Rajesh Garg, Jul 22, 2003, in forum: C++
    Replies:
    11
    Views:
    814
    Mike Wahler
    Jul 24, 2003
  4. Carl Forsman
    Replies:
    0
    Views:
    1,458
    Carl Forsman
    Nov 19, 2008
  5. Charblit
    Replies:
    0
    Views:
    494
    Charblit
    Jan 23, 2010
Loading...

Share This Page