textfile usage...
MSGleader

ive been looking around alot on functions for using text files, and ive found some stuff, but its not really straight forward... ive found these functions, but im not sure which is the best to use...

fputs();
fgets();
fopen();
fwrite();
fread();
fclose();

so far ive figured out how to use fopen, fwrite, and then fclose to close it... but im having troubles on fread, and i dont think im doing fwrite properly...

i was gonna have some text read from a file, and printed to the screen using textprintf_ex. but i couldnt get fread to work...

can someone please explain to me how fwrite, and fread work?

CGamesPlay

Please show the code you've got, and we can go from there.

Jonny Cook
PACKFILE *pf = pack_fopen("file.txt", "w");  // open file for writing
char data[] = "Hello world!";
pack_fwrite((void *)data, strlen(data), pf);
pack_fclose(pf);

That would write the string "Hello world!" to a file called "file.txt". To read the string back from the file you would do the following:

PACKFILE *pf = pack_fopen("file.txt", "r");  // open for reading
if (pf) {  // if pf == NULL, the file does not exist
    char data[256];
    int i;
    for (i = 0; !pack_feof(pf); ++ i) {
        data<i> = pack_getc(pf);
    }
    data[i + 1] = 0;
}
pack_fclose(pf);

MSGleader
        char words[5];
  FILE *thefile = fopen("stuff.txt","a");
  fwrite("Joely",5,1,thefile);
  fclose(thefile);
  thefile = fopen("stuff.txt","r");
  while (!key[KEY_ESC]) {
          if(key[KEY_ENTER]){
                             fread(words,5,1,thefile);
                             textprintf_ex(screen,font,10,10,makecol(255,255,255),-1,"Text file contents: %s",words);
                             }
  }

^I was just experimenting... but thats what i have in my code...
ive gotten fread to sortof work now...
and im not using packfiles... will it still work?

Evert

For the love of Shawn don't use fread and fwrite!
To read a line from a textfile, use fgets(). If you need to chop it up in smaller parts, you can do that afterwards.
To write lines to a testfile, just use fprintf(). fwrite() is probably ok for a character string, but I tend to make the logical distinction between binary data and text (which is of course stored as binary data) and stick with using the proper I/O functions for either.

Tobias Dammers

The differences between binary and text files, from a C point of view, are rather confusing for beginners. In fact, the only reason why they are treated differently is the fact that different platforms expect different codes for line breaks (LF or CR or both), which the i/o functions handle for you if it's a text file, but not if it's a binary file.
After all, a string in C is nothing but an array of chars, so you can just read the characters one by one using fgetc. Sometimes, this can be the most straightforward solution, especially when you don't care about line breaks (for example, a C parser would ignore line breaks, since all whitespace is treated equal in C - at least after preprocessing).
The most important reason for not using fread / fwrite is that they read/write memory contents to/from disk "as-is", without taking into account the fact that different platforms use different (incompatible) internal data representations. For example, C mandates that a char has at least 8 bits, but it may have more.

MSGleader

ok, so ive gotten fprintf to work, and ive gotten the fgets to work... the only problem im having now is when i use return(enter) in the text file... this happens

The text file:

struct HumanPlayer
{
int x,y;
int alive;
int life;
}player;

This is what prints to the screen...

struct HumanPlayer^
{^
int x,y;^
int alive;^
int life;^
}player;

how come it keeps putting a stupid ^ symbol for every return i use?
it wont do that when i actually load the text file for my game code will it?

Fladimir da Gorf

I bet it's the carriage return character, which is at the end of every line in windows. However, it should be removed if you use text mode instead of binary mode.

Tobias Dammers

...in fact, this is the only thing you need "text files" for.

Thread #578742. Printed from Allegro.cc