Recent

Author Topic: KOL-CE File Write  (Read 6928 times)

JoniDS

  • New Member
  • *
  • Posts: 11
KOL-CE File Write
« 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

aji

  • New Member
  • *
  • Posts: 13
Write to file
« Reply #1 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]

stootch

  • New Member
  • *
  • Posts: 14
Re: KOL-CE File Write
« Reply #2 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.

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 152
KOL-CE File Write
« Reply #3 on: March 22, 2008, 05:30:07 pm »
RTFM.
Code: [Select]
FileWrite(f, PChar(msg)^,Length(msg));

JoniDS

  • New Member
  • *
  • Posts: 11
KOL-CE File Write
« Reply #4 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

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 152
KOL-CE File Write
« Reply #5 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));

JoniDS

  • New Member
  • *
  • Posts: 11
KOL-CE File Write
« Reply #6 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.