160 likes | 229 Views
HELLO My Name is. Theo Jenetopulos William Murcia Melissa Sarduy Bruce Eckel. Freads / Fwrites. Freads / Fwrites. Block Diagram. Block Diagram. Example. / * fwrrd.c */ #include < stdio.h > #include < math.h > #define SIZE 20 void main ()
E N D
HELLO My Name is Theo Jenetopulos William Murcia Melissa Sarduy Bruce Eckel
Example /* fwrrd.c */ #include <stdio.h> #include <math.h> #define SIZE 20 void main() {int n; float d[SIZE]; FILE *fp; for( n = 0; n < SIZE; ++n ) /* Fill array with roots. */ { d[n] = (float)sqrt( (double)n ); }fp = fopen( "data", "w+" ); /* Open file. */fwrite( d, sizeof( float ), SIZE, fp ); /* Write it to file. */ rewind( fp ); /* Rewind file. */fread( d, sizeof( float ), SIZE, fp ); /* Read back data. */ for( n = 0; n < SIZE; ++n ) /* Print array. */ {printf( "%d: %7.3f\n", n, d[n] ); }fclose( fp ); /* Close file. */ }
Lessons Learned Freads/Fwrites -Is Binary stream input / output -Use it to save and read -For reading and writing in binary mode, I have learned that there are functions such as fread() and fwrite(). fread() takes a pointer to a location in memory where adequate space has been allocated. -The function fread() reads elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.
Lessons Learned Fgets/Fputs Is Line input / output The function char *gets(char *line) reads the next line of text (i.e. up to the next \n) from the standard input and places the characters (except for the \n) in the character array pointed to by line. The function char *fgets(char *line, int max, FILE *fp) is somewhat more useful. It reads the next line of input from the stream fp and places the characters, including the \n, in the character array pointed to by line. The function int puts(char *line) writes the string pointed to by line to the standard output, and writes a \n to terminate it. The function intfputs(char *line, FILE *fp) writes the string pointed to by line to the stream fp. Like puts, fputs returns a nonnegative value or EOF on error. Unlike puts, fputs does not automatically append a \n.