File I/O

Started by MachineElf, Tue 15/07/2003 21:41:25

Previous topic - Next topic

MachineElf

Can anyone explain to me how the File I/O in AGS really work?
There seem to be no way of reading a specific line in a file or even reading more than one thing from a file, or am I getting this all wrong?

An examle: Say I have a file called bice.dat that looks something like this (I guess the file format looks somewhat different but never mind for now):

"This is a string
5 10
15
-- EOF --"

Now I'd like to read the first line into a string and the numbers into three different integers. Would this be possible in any way? And how do I make a file that actually look like this?

If anyone wonders I'm once again trying to expand the limits of AGS a little, this time by making a little play-by-email game...
There are 10 kinds of people in the world: Those who understand binary and those who don't.

Scorpiorus

#1
ok, making such a file.....

int file = FileOpen("bice.dat", FILE_WRITE);
if (file == 0) { Display("error creating a file"); QuitGame(0); }
else {
FileWrite(file, "This is a string");
FileWriteInt(file, 5);
FileWriteInt(file, 10);
FileWriteInt(file, 15);
FileClose(file);
}


reading from file...
string string_buffer;
int value1, value2, value3;

int file = FileOpen("bice.dat", FILE_READ);
if (file == 0) { Display("error opening file"); QuitGame(0); }
else {
FileRead(file, string_buffer);
//a little correction here
value1 = FileReadInt(file);
value2 = FileReadInt(file);
value3 = FileReadInt(file);

FileClose(file);
}


but keep in mind that these functions write data in the special AGS format.

-Cheers

MachineElf

Ah, yes of course. The "cursor" moves in the file when reading and writing, just like in C.

Well, thanks!

Is there anyway to perform a command that lists all .dat-files (or just all files) in the game directory? I can't seem to find any function for this.
There are 10 kinds of people in the world: Those who understand binary and those who don't.

Scorpiorus

QuoteAh, yes of course. The "cursor" moves in the file when reading and writing, just like in C.
aha, exactly.

QuoteIs there anyway to perform a command that lists all .dat-files (or just all files) in the game directory? I can't seem to find any function for this.
there is no such one in files section but you can look at ListBoxDirList() function. You need to create a listbox then and retrieve the files names via ListBoxGetItemText() command. ;)

-Cheers

MachineElf

Yeah, that does it! Thanks a lot!
There are 10 kinds of people in the world: Those who understand binary and those who don't.

SMF spam blocked by CleanTalk