CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,632

    ifstream and nulls

    Sorry, this is probably a basic question but I've always used MFC for I/O. I'm using an ifstream and it stops reading as soon as it hits a null. I'm trying to read the end of a file. How do I get it to read everything and not stop at the null. Here's my code. Thanks
    Code:
      std::ifstream ifs;
      ifs.open(m_FilePath, std::ifstream::in | std::ifstream::binary);
      ifs.seekg(-2048l, ifs.end);
      char buf[2048];
      ifs.read(buf, 2048l);

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,608

    Re: ifstream and nulls

    buf is sized at 2048, but 20481 chars are tried to be read??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.2.0)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,632

    Re: ifstream and nulls

    That's a letter l, not a 1. It works for files without a null, but stops where there's a null.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,632

    Re: ifstream and nulls

    Turns out because I'm reading into a char array, the debugger isn't showing anything after the null. The data is there though

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,608

    Re: ifstream and nulls

    You could read it into a std::string as std::string can contain nulls. Something like:

    Code:
    std::ifstream ifs;
    std::string buf(2048, 0);
    
    ifs.open(m_FilePath, std::ifstream::in | std::ifstream::binary);
    ifs.seekg(-2048, ifs.end);
    ifs.read(buf.data(), 2048);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.2.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured