Reading, Writing Txt Files


TrevorBradley

Active Member
Joined
Nov 6, 2007
Messages
732
I'm trying to figure out a way to read and write plaintext txt files directly from the filesystem using fenix.

I'm aware there are load, save, fopen, fclose functions, but it appears that Fenix strings saved to file are prefixed with a few characters. If I try to read a txt file from the filesystem, the first few characters are removed.

For instance, this code:

CODE
Program SaveTest;
Global
int my_file;
String data;
Begin
my_file=fopen("output.txt",O_READ);
fread(my_file,data);
fclose(my_file);
write(0,0,0,0,data);

Repeat
frame;
Until (key(_enter));

let_me_alone();
exit();

End;


Reads in a txt file containing "0123456789" and prints to the screen: "456789". (I know, the load function does the same thing in far shorter code...)

Is there any way I could read the data directly as plaintext without losing any characters? Perhaps as an array of bytes?

Thanks in advance.
 
In Fenix by use of fread(). Read byte per byte. The reason strings don't work is because they are saved and loaded in the format <INT length><CHAR[] chars>, which makes sense because otherwise Fenix would have no way of knowing how large chars is. Yes, null terminating would also have been an option, but then you can't save a string with a null terminator. Yes escaping it would also have been possible, but then you can't save that combination. Anyway, this solution is good.

Much more text editing like functions are fgets() and fputs(), which save/load line by line.
 
Back
Top