Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » textfile usage...

This thread is locked; no one can reply to it. rss feed Print
textfile usage...
MSGleader
Member #6,637
December 2005

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
Member #2,559
July 2002
avatar

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

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Jonny Cook
Member #4,055
November 2003

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);

The face of a child can say it all, especially the mouth part of the face.

MSGleader
Member #6,637
December 2005

        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
Member #794
November 2000
avatar

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
Member #2,604
August 2002
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

MSGleader
Member #6,637
December 2005

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
Member #1,565
October 2001
avatar

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.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Tobias Dammers
Member #2,604
August 2002
avatar

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

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: