![]() |
|
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(); 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
![]() |
Please show the code you've got, and we can go from there. -- 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
|
^I was just experimenting... but thats what i have in my code... |
Evert
Member #794
November 2000
![]() |
For the love of Shawn don't use fread and fwrite! |
Tobias Dammers
Member #2,604
August 2002
![]() |
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. --- |
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? |
Fladimir da Gorf
Member #1,565
October 2001
![]() |
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
![]() |
...in fact, this is the only thing you need "text files" for. --- |
|