Open File -> Display In Memo

Ok. So I'm very new on this and I'm sure the answer is insanely simple.
I'm wanting to open any file (image, zip, etc) and convert that to a string so I can put it in a memo.
Actually, I just need it in a string. Memo part is just for testing.

My code is this right now.
(Pretty much exactly from http://www.functionx.com/bcb/fileprocessing/vclfilestream.htm )

[code]void __fastcall TForm1::btnOpenClick(TObject *Sender)
{
TFileStream *FStream;

if( OpenDialog1->Execute() )
{
try {
FStream = new TFileStream(OpenDialog1->FileName,
fmOpenRead | fmShareExclusive);
//FStream->ReadComponent(Memo1);
//Form1->Memo1->Lines->Add(FStream);
}
__finally
{
delete FStream;
}
}
}[/code]
The commented out lines are the 2 that I've tryed.
I'm having a hard time understanding how TFileStream actually works even though I've searched online...
Thank You.

Comments

  • Form1->Memo1->Lines->LoadFromFile("myfile.xyz");

    It doesn't make any sense to load binary files such as images and zip files that way though... you will only get gibberish as the Memo expects text strings.
  • : Form1->Memo1->Lines->LoadFromFile("myfile.xyz");
    :
    : It doesn't make any sense to load binary files such as images and
    : zip files that way though... you will only get gibberish as the Memo
    : expects text strings.

    Thank you. But that seems to only load the header of the file.
    (Only loads 'GIF89a0' when opening a gif)
    (Or maybe that's because that's all the memo can display?)

    And as I said, putting it into a memo is just for testing.
    (And with LoadFromFile, I can't seem to use it as a function by itself). I'm just trying to get it into a string. The reason is I'm going to be sending it in a POST with IdHTTP to a php script...
    I'm coming from vb6 actually, where I was accustomed to winsock.

    Anyway, I feel much closer as I couldn't even get the header before.
    And I'm looking at the help file, and it looks like LoadFromFile uses TFileStream.
    Hmm...

    -----------
    Edit/Update

    I read some more ended up getting all the data to show in a RichEdit by turning PlainText off.

    [code] if(OpenDialog1->Execute()) {
    Form1->RichEdit1->PlainText = false;
    Form1->RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
    }[/code]

    So it works like that.
    But I really don't need to load it directly into RichEdit.
    And I can't just do 'LoadFromFile(OpenDialog1->FileName)'

    Hmm...
  • You should be able to use it stand-alone, I think it is static.

    TStrings::LoadFromFile(...)

    But as I told you, it doesn't make sense to read a binary file as text. The binary likely contained a binary zero somewhere and the program interpreted it as '' (null termination of string).
  • : You should be able to use it stand-alone, I think it is static.
    :
    : TStrings::LoadFromFile(...)
    :
    : But as I told you, it doesn't make sense to read a binary file as
    : text. The binary likely contained a binary zero somewhere and the
    : program interpreted it as '' (null termination of string).

    It says I need a . or -> before it.

    Anyway. I think I might be taking the wrong route...
    I'm looking for the best way to send a file through a POST to a php script.
    I'm trying to go through IdHTTP, is there a better control for this task?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories