-
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? :confused:
-
Methinks it's this.
Text[Length + 1] = '\0';
Remember AnsiString are 1 referenced, while c strings are zero referenced.
-
>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
-
I haven't used AnsiString or similar objects myself, but this definitely looks like your outside its index:
Text[Length + 1] = '\0';
-
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?
-
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;
}
-
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++
-
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.
-
>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
-
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.
-
>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
-
Awesome - Bill Engvall, When you're left in all an wonder!
:D