/* files.c demonstrates simple file I/O in C */ #include main() { FILE *fp; char buf[1024]; // open file for writing if (( fp = fopen( "myfile.dat","w" )) == NULL ) { perror( "error opening myfile.dat" ); exit( 1 ); } // write message to file fprintf( fp,"hello file!\n" ); // close file fclose( fp ); // open file for reading if (( fp = fopen( "myfile.dat","r" )) == NULL ) { perror( "error opening myfile.dat" ); exit( 1 ); } // read first word in file fscanf( fp,"%s",buf ); // output first word to the screen printf( "buf=[%s]\n",buf ); // close file fclose( fp ); } // end main()