Page 1 of 1

PostPosted: Sun Apr 04, 2004 2:47 am
by raveen106
For writing a string to file, we have writechars/writebyte in PocketC. To get the string back is there any single command? Similarly for writing and reading int and float numbers to files which commands can be used ? can sombody show me sample code covering the above?



PostPosted: Mon Apr 05, 2004 10:15 pm
by wolfgang

integers can be written/read with writeint() and readint(). Please read the PocketC documentation and/or built-in help for those functions.

To write/read a string (terminated with a newline \n), see the code below. To write/read a float, use the same code, PocketC's automatic type conversion will handle conversion between float and string for you.

There are faster, elegant ways to write/read the string, but the code below works reasonably well. Search the PocketC webboard, Guy Sprackland has posted a lot about parsing files.


// write a string to a file, terminate it with a newline
filewrite_nl (int i_fh, string s_string)
return (filewrite (i_fh, s_string + "\n", strlen (s_string) + 1));

// read an single line of text (terminated by a newline)
fileread_nl (int i_fh)
char c;
string s;

while ((c = readbyte (i_fh)) != '\n')
s = s + c;

return (s);

Composing music on the PocketPC! -