ʊIrishancest
Legally Certified Warthog Operator
Hey. Do you know c? And if you do, can you help me? I'm having problems working with text files. I've never been able to implement one, and until i do so successful i doubt i'll actually understand it. Thanks a lot.
04.15.08 6:05 am

Replies
ʊCoolhand2
Kills People While They Type
The functions you'll need to look up are fopen, fread, fwrite, fgetc, fputc, and fclose. They all use the FILE pointer type (which is built into C) except for fgetc and fputc.
A sample program would be something like:
#include <stdio.h>
int main( void )
{
/* The file handler pointer */
FILE *fhp;
/* The buffer we'll read the file into */
char[80] str;
int i; /*You'll see this used in a bit*/
/* Open up a random file in read-only mode */
fhp = fopen( "random.file", "r" );
fread( str, 1, 80, fhp );
/* Alternative way:
* Uses a counter. Though be careful with
* this, as it waits for EOF from the file
* before it stops, and that could lead to
* a buffer overflow problem. If done
* correctly, you can still do this though.
* for( i = 0; str = fgetc( fhp ); i++ );
*/
/* This writes to a file if you want to
* There are two ways to do this:
* for( i = 0; str && fputc( str, fhp ); i++ );
* and
* fwrite( str, 1, 80, fhp );
*/
/* And now we close the file */
fclose( fhp );
}
For details on the functions I used, you can start here: http://www.cppreference.com/stdio/fopen.html
Remember, the answer is always 42.