Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileRead

Status
Not open for further replies.

exodus300

Programmer
Joined
Mar 22, 2002
Messages
262
Location
AU
I have program which loads and saves file. When saving, I use the following:
Code:
AnsiString Text = "bla bla bla";
int TextSize = Text.Length();
int FileHandle = FileCreate("c:\\something.txt");
FileWrite(FileHandle, (char *)&TextSize, sizeof(TextSize));
FileWrite(FileHandle, Text.c_str(), TextSize);
FileClose(FileHandle);
This stores the size of the text, then the text itself. This works OK, but how do I read the text again into an AnsiString? eg.
Code:
AnsiString RetrievedText;
int TextSize;
int FileHandle = FileOpen("c:\\something.txt", fmOpenRead);
FileRead(FileHandle, (char *)&TextSize, sizeof(TextSize));
//What goes here to read the string into RetrievedText?
FileClose(FileHandle);
I have tried various methods, but I get either blank strings or access violations. Please help.
 
First try to use
FileRead(FileHandle, (char *)&Text, sizeof(TextSize)-1);

If It does not work, I think your problem results from using a parameter of AnsiString type. If you use FileRead function as below, It must work.

int FileHandle;
int TextSize;
int iBytesRead;
char *Buffer;

FileHandle = FileOpen("c:\\something.txt", fmOpenRead);

FileSeek(FileHandle,0,0);
Buffer = new char[TextSize];
iBytesRead = FileRead(FileHandle, Buffer, TextSize-1);
FileClose(FileHandle);


 
THANK YOU!
This is working, but I still have problems. In my file I have an int (32-bits or 4 bytes) showing the length of the string. Immediately after that is the actual string. My program is reading the number right (I tested this with a ShowMessage, and each time the for loop repeats, it shows the correct number), but the strings have 'junk' characters at the end of them. And I know FileRead is reading the right number of bytes, because the part of the strings that is not garbled chars is correct. I converted the strings to AnsiString using AnsiString(char *Whatever), but this isn't the problem because it also happens with Application->MessageBox, which takes a char*.
What must I do to get this right????

(If you like, I can give you an extract of my source code [the relevent parts] and a copy of the file I am trying to read from, if this will help you help me....)

[pc3]
In the end it will probably be the simplest, most obvious thing I was doing wrong...
 
Just a guess, but when I have a problem
with strings I first look at the size of
the buffer I am saving them in. You might
try increasing the size of the buffer by 1
or decreasing it by one. If you have already
consideered this well never mind.
 
I tried decreasing it , but not increasing. I will have a look.... [pc3]
In the end it will probably be the simplest, most obvious thing that I was doing wrong...
 
HMMM... I WAS just about to send you my source when I noticed something VERY strange... The "problem code" is written in a DLL function. I copied the code into a regular exe project, and realised that the problem had magically disappeared.

Could the fact that it's in a DLL be anything to do with it?

Anyways, here's my source... (Put it in a new project, then a DLL... see for yourself!)


NOTE: You need certain header files #included to compile this.. I don't know which one, but it is one of the following:
Code:
vcl.h
Classes.hpp
Controls.hpp
StdCtrls.hpp
Forms.hpp
ComCtrls.hpp


Code:
    int iFileHandle = FileOpen(FileName, fmOpenRead);
    if (iFileHandle == -1) {
    	Application->MessageBox("Error: Unable to create file handle.", "Error",
        	MB_ICONERROR);
        return false;
        }

    AnsiString AppVersion;
    int iAppVerSize;

    FileRead(iFileHandle, (char *)&iAppVerSize, sizeof(iAppVerSize));

    char *pszBuffer = new char[iAppVerSize];
    FileRead(iFileHandle, pszBuffer, iAppVerSize);
    AppVersion = AnsiString(pszBuffer);
    delete pszBuffer;

    Application->MessageBox(AppVersion.c_str(), "Version", MB_ICONINFORMATION);
    // This is where the prob is - has an extra symbol (usually &) appended to the string
    // (BUT only when this code is in a DLL, not in a regular project)

    /*
    MORE CODE...
    .
    .
    .
    */

    FileClose(iFileHandle);
[pc3]
In the end it will probably be the simplest, most obvious thing that I was doing wrong...

[smarty] [tt]One is glad to be of service
- Bicentennial man[/red][/tt]
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top