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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ansistrings FileRead

Status
Not open for further replies.

Tremorblue

Programmer
Apr 30, 2004
56
GB
I want to load from a file directly into an ansistring how is it possible ?

At the moment I am using FileRead.

The data I am loading is binary and needs to be passed into a function that uses an ansistring.

I could load the data into a char array but cannot do
a simple conversion i.e
ansistring = array as the array is not null terminated. Nulls could appear anywhere in the array.

 
Try using the .c_str() function.
Code:
char t = "A string.";
AnsiString X;
X.c_str() = t;

James P. Cottingham
[sup]
There's no place like 127.0.0.1.
There's no place like 127.0.0.1.
[/sup]
 
c_str() is a function and can't be assigned a value. You can cast the character pointer to an ansistring though.

char CharBuf[100];
AnsiString Value;

FileRead(FilePtr, &CharBuf, 99);
CharBuf[100] = NULL;

Value = (AnsiString)CharBuf;

I can't remember, but I don't think you even need the AnsiString casting...the operator overloading handles it I thought.

Chris
 
Supernat03,
[tab] You're right. I realized that after I posted it but I waited to see if there was a response before clarifing. You could also use (I beleive) strcpy(X.c_str, t).


James P. Cottingham
[sup]
There's no place like 127.0.0.1.
There's no place like 127.0.0.1.
[/sup]
 
Thanks for the response.

Remember that the file I am reading is binary so could contain nulls at any point. So I read a 1000byte file and the position 10 has a null or zero. I believe your
Value = (AnsiString)CharBuf; would only copy the first 10 characters, I want all 1000 copied.
 
Howdy,

First of all, what does the function you want to pass the AnsiString to do? Would it be simple to create a similar routine that accepts an array?

If the function you want to pass to is a function you wrote, that should be fairly straightforward...

If not, then there may be (somewhere) an similar routine using an array. Someone, somewhere, at some point in time, has probably done something similar to what you are doing.

Oh well, Good luck,
onrdbandit

No! Try not. Do, or do not. There is no try. - Yoda
 
why would ansistring be preferable to array
if I am accessing binary I am not concerned with string
but am merely accessing individual unique character lists or
arrays if you prefer and not strings as they are comonly used.
 
and lastly

is the function that you are passing the data locking you into a coding process that could otherwise be rewritten utilizing arrays. if the null is apt to appear anywheere
you can not be displaying the information but are more likey using the attributes of the ansi string to manipulate the data. It may be an oportunity to practise the actual parsing of the character list by code other than that available in ansistring. a little more code for you but perhaps more straighforward.

I could be wrong though.

tomcruz.net
 
Thanks for the help... The code below seems to do the trick.

AnsiString aOutMessage;
int iFileHandle = FileOpen(aFileName, fmOpenRead);
int iFileLength = FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
char *sMessage = new char[iFileLength+1];
FileRead(iFileHandle,sMessage,iFileLength);
FileClose(iFileHandle);
for(int x=0;x<iFileLength;x++)
aOutMessage+=sMessage[x];

 
try this:
Code:
TStringList *MyList;
MyList = new  TStringList ();
MyList->LoadFromFile ("The Path of yor file");
AnsiString Any;
Any = MyList->Text;
check for bugs
I wrote this on the air

--- LastCyborg ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top