Lazarus

Programming => Packages and Libraries => KOL => Topic started by: JoniDS on March 20, 2008, 10:40:08 pm

Title: KOL-CE File Write
Post by: JoniDS on March 20, 2008, 10:40:08 pm
hi,

I'am trying to write to a file in my win ce application.

This is the code :

//CODE

procedure TForm1.Button1Click(Sender: PObj);
var
dir : string;
f : thandle;
 msg : String;
begin
msg := 'hello world';

dir := extractfilepath(paramstr(0));

f := FileCreate(dir+'xpto.txt', ofCreateNew);
FileClose(f);
f := FileCreate(dir+'xpto.txt', ofopenwrite);
FileWrite(f, msg,sizeof(msg));
FileClose(f);

end;  

//CODE

It creates the file, but it writes nothing in it.
Can you tell me what's wrong?

Thanks in advance
Title: Write to file
Post by: aji on March 20, 2008, 11:05:00 pm
This is how I write a line of text to a file that already exists:

[code]
 var n,i: integer;
     F : Textfile;
     buffer: string;
begin
     buffer:='';
     Assign(F, '\My Documents\test.txt'  );
     rewrite(F);
     Writeln(F,buffer) ;
     buffer:='';
     CloseFile(F) ;
[/quote]
Title: Re: KOL-CE File Write
Post by: stootch on March 21, 2008, 06:57:43 am
Quote from: "JoniDS"

Code: [Select]

f := FileCreate(dir+'xpto.txt', ofCreateNew);
FileClose(f);
f := FileCreate(dir+'xpto.txt', ofopenwrite);
FileWrite(f, msg,sizeof(msg));
FileClose(f);


What GetLastError says after FileWrite?
Also, you don't have to close and instantly reopen the file once you have a handle.
Title: KOL-CE File Write
Post by: yuriy_sydorov on March 22, 2008, 05:30:07 pm
RTFM.
Code: [Select]
FileWrite(f, PChar(msg)^,Length(msg));
Title: KOL-CE File Write
Post by: JoniDS on March 23, 2008, 07:46:29 pm
Thanks,
I would gladly read the manual where is it?

Thanks for the tip :

FileWrite(f, PChar(msg)^,Length(msg));

However this way it still writes nothing to the file.
After some faild tentatives i managed to get it working by doing the following :
f := FileCreate(dir+'xptoa.txt', ofopenwrite or ofopenalways);

Not to abuse your help but now how do i write and read a RECORD?

Thanks again
Title: KOL-CE File Write
Post by: yuriy_sydorov on March 24, 2008, 09:39:16 pm
Quote from: "JoniDS"
Thanks,
I would gladly read the manual where is it?


I meant generic Object Pascal info how to pass string content to functions which expects buffer and its size.
In your initial approach you tried to write 4-byte pointer to a string...

Quote from: "JoniDS"

Not to abuse your help but now how do i write and read a RECORD?

Thanks again

This time your approach will work:
Code: [Select]
FileWrite(f, myrec, sizeof(myrec));
Title: KOL-CE File Write
Post by: JoniDS on March 25, 2008, 02:06:22 am
Thank you very much for your help, it's working now.
And i would like to congratulate you for the excellent  work at KOL-CE.
TinyPortal © 2005-2018