Loading a file... but there is a small error

This is a discussion on Loading a file... but there is a small error within the C++ Programming forums, part of the General Programming Boards category; The comments are in danish so you probably wont understand them, but since the code is fairly simple I don't ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    Loading a file... but there is a small error

    The comments are in danish so you probably wont understand them, but since the code is fairly simple I don't think it will be a problem.

    The error is this:
    If you are to load a file that contains nothing but a sign (like 'g') and exactly "one" new line (that is '/n' - though it with dos is "/r/n") then there will be added one more sign to the loaded text -WHY!

    Code:
    AnsiString TTextFile::Load() const
    {
      AnsiString Buffer;
      int Length;
    
      ifstream File(itsPath.c_str());
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
    
        Length = File.tellg();
    
        Buffer.SetLength(Length + 1);
    
        File.clear(); /* File nulstilles så funktioner som eof(), fail(), good() og bad() retunere det som de altid gør når en
                         ny stream bliver lavet. */
        File.seekg(0, ios::beg); /* File's itterator (pointeren der peger på det tegn der ved næste kald skal læses eller overskrives)
                                    //bliver sat tilbage til starten af filen */
    
        File.read(Buffer.c_str(), Length);
        Buffer[Length + 1] = '\0';
    
        File.close();
      }
    
      return Buffer;
    }
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    If the length of the buffer is Length+1, then Length+1 is an invalid index.
    If you read Length bytes, then the buffer will be filled from [0] to [Length-1]. So you want to put the null terminator at [Length].

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Yeah you might think so, but this code is made with borlands AnsiString instead of string, and when you use AnsiString it starts with 1 instead of zero. - Besides... I've checked.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    std::string str;
    getline(File,str,EOF);
    AnsiString Buffer(str.c_str());
    potental problems:
    1. If file is opened for text this will stop at an embeded EOF char for your environments conventions (likely ^Z or ^D)
    2. This will stop at an embeded (char)-1
    3. AnsiString's constructor must take a char* and will stop on an embeded '\0' (though std::string has no problem here)

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    I've tested the following three while loops and it is only the one Salem have shown me that actually works without reading to few or to many characters.
    Why is that?
    Besides... I thought get() returned istream& and not something that could prove to be usefull in a if/while etc. statement.
    Code:
        while (File.get(ch))
        {
          File.get(ch);
          Buffer[i++] = ch;
        }
    
        while (i <= Length && File.good())
        {
          File.get(ch);
          Buffer[i++] = ch;
        }*/
    
        while (i <= Length && File.get(ch))
        {
          Buffer[i++] = ch;
        }
    Furthermore I find it a bit funny that write() worked out allright but not read().

    Code:
      if (File.is_open())
      {
        File.write(str.c_str(), str.Length());
    
        File.close();
      }
    Last edited by Zahl; 04-27-2003 at 08:40 AM.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    This FAQ describes the problem you have observed, except it demostrates the issue in C, not C++.

    gg

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    > I thought get() returned istream& and not something that could prove to be usefull in a if/while etc. statement

    The istream that is returned by get() is evaluated by the if/while statement which will call the "operator void *()" method of the istream. This overloaded operator returns: "fail() ? 0 : (void*)this)" - which makes it very usefull in if/while statements.

    gg

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    As a last question (on this thread ) why the i <= length? Wouldn't File.get(ch) not always end the loop when needed?
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21