File_Processing(英文版)(ppt 44页).pptVIP

  • 3
  • 0
  • 约1.32万字
  • 约 44页
  • 2016-02-07 发布于湖北
  • 举报
File_Processing(英文版)(ppt 44页).ppt

* /* Fig. 11.11: Creating a randomly accessed file sequentially , with empty structs. */ #include stdio.h struct clientData { int acctNum; char lastName[ 15 ]; char firstName[ 10 ]; double balance; }; int main() { int i; struct clientData blankClient = { 0, , , 0.0 }; FILE *cfPtr; if ( ( cfPtr = fopen( credit.dat, w ) ) == NULL ) printf( File could not be opened.\n ); else { for ( i = 1; i = 100; i++ ) fwrite( blankClient, sizeof( struct clientData ), 1, cfPtr ); fclose( cfPtr ); } return 0; } Unformatted File I/O Functions fwrite - Transfer bytes from a location in memory to a file size_t fwrite(const void * buffer, size_t size, size_t nmemb, FILE * fp); fread - Transfer bytes from a file to a location in memory size_t fread(void * buffer, size_t size, size_t nmemb, FILE * fp); * Unformatted File I/O Functions Example: fwrite( number, sizeof( int ), 1, myPtr ); Number an integer variable number location to transfer bytes from sizeof( int ) number of bytes to transfer 1 for arrays, number of elements to transfer In this case, one element of an array, i.e. one number is being transferred myPtr - File to transfer to fread similar * Unformatted File I/O Functions fwrite( myObject, sizeof (struct myStruct), 1, myPtr ); To write a data block with designated size to a file sizeof – return the size in bytes of the object in parentheses To write several array elements Pointer to array as first argument Number of elements to write as third argument * * /* Fig. 11.12: Writing to a random access file*/ …. int main() { FILE *cfPtr; struct clientData client = { 0, , , 0.0 }; if ( ( cfPtr = fopen( credit.dat, r+ ) ) == NULL ) printf( File could not be opened.\n ); else { …… while ( client.acctNum != 0 ) { printf( Enter lastname, firstname, balance\n? ); fscanf( stdin, %s%s%lf, client.lastName, client.firstName, \ client.balance ); fseek( cfPtr, ( client.acctNum - 1 )

文档评论(0)

1亿VIP精品文档

相关文档