What's wrong with this

This is a discussion on What's wrong with this within the C++ Programming forums, part of the General Programming Boards category; Now heres a challenge. I've sort of asked about the same thing before but no one could give me an ...

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

    Unhappy What's wrong with this

    Now heres a challenge. I've sort of asked about the same thing before but no one could give me an ansver on how it is so. (PROBLEM: function does not return what it's supposed to.)

    Code:
    AnsiString TFileHandle::Load() const
    {
      AnsiString Return;
      char *Text;
      int Length;
    
      ifstream File(itsName.c_str());
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.seekg(0, ios::beg);
    
        Text = new char[Length + 1];
        Text[0] = '\0';
    
        File.read(Text, Length);
        Text[Length + 1] = '\0';
    
        Return = Text;
        delete [] Text;
    
        File.close();
      }
    
      return Return;
    }
    The ansver for why I use AnsiString is because it's more "workable" with all those objects which Borland offers. Still I have a pointer to a char since that's what demands as an argument. BTW itsName is private and #include "fstream.h" is in the objects header.

    Now why is it that what thÝs function return is what's in the file PLUS a few other REAL strange "signs" (with a ShowMessage I have tryed what Text equals just before Return = Text but allso there it have the same odd signs and I kind of expect read to work). Other then that delete [] Text give me an error which doesn't make ANY sence since the AnsiString's operator=(char*) copys what's in the array it doesn't just point at it.

    Now can anyone ansver BOTH questions?
    Last edited by Zahl; 10-20-2002 at 09:47 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

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Methinks it's this.

    Text[Length + 1] = '\0';

    Remember AnsiString are 1 referenced, while c strings are zero referenced.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Now why is it that what thÝs function return is what's in the file PLUS a few other REAL strange "signs"
    The first change you should make is to avoid an off-by-one error which should cause a seg fault or print several odd characters. Change this:
    Code:
    Text = new char[Length + 1];
    Text[0] = '\0';
    
    File.read(Text, Length);
    Text[Length + 1] = '\0';
    To this:
    Code:
    Text = new char[Length + 1];
    Text[0] = '\0';
    
    File.read(Text, Length);
    Text[Length] = '\0';
    The delete[] operation is giving you an error for the above reason. Lose the off-by-one error and both of your problems will go away.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I haven't used AnsiString or similar objects myself, but this definitely looks like your outside its index:
    Text[Length + 1] = '\0';
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Okay now I've change that part (which I've tryed before) and it didn't help with the return part though the delete part it did take care of, but that wasn't the BIG problem.

    Could '\n' (enters) have anything to do with it so that Length doesn't get the length it's supposed to?
    Last edited by Zahl; 10-20-2002 at 10:51 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
    Join Date
    Oct 2002
    Posts
    160
    Code:
    AnsiString TFileHandle::Load() const
    {
      AnsiString Return;
      char *Text;
      int Length;
    
      ifstream File(itsName.c_str());
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.seekg(0, ios::beg);
    
        Text = new char[Length + 1];
        Text[0] = '\0';
    
        File.read(Text, Length);
        Text[Length] = '\0';
    
        Return = Text;
        delete [] Text;
    
        File.close();
      }
    
      return Return;
    }
    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

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Try this :
    Code:
    char ch;
    int i = 0;
    ..
    ..
    while ( File.good() ) { // EOF or failure stops the reading
    	File.get( ch );
    	if( !ch ) break; // quit on null
    	Text[i++] = ch;
    }
    Text[i] = '\0';
    ..
    ..
    edit : changed to i++

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    I know that... but why can't I make it work with read which would be a much more "nice" code. Besides I'll like to know what's wrong since it really shouldn't be wrong.
    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

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Besides I'll like to know what's wrong since it really shouldn't be wrong.
    Try changing your program to use char *'s or std::strings and see if you have the same problem. As far as I can tell from your code (unless I'm missing something obvious, which does happen), there's a problem intializing Return with Text, because it works just fine for me using either char * or std::string for Return.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    The performance will be the same if I use "my" method as if I use The dog's right? Because if that's the case this is what I'll do.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The performance will be the same if I use "my" method as if I use The dog's right?
    Probably not, reading large blocks in a single read is usually more efficient than reading character by character. And of course, if your method doesn't give the correct output but The Dog's does, there's no contest is there? If the output is incorrect then it is irrelevant how fast you get it.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Awesome - Bill Engvall, When you're left in all an wonder!

    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. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

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