- 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 )
您可能关注的文档
最近下载
- 心理健康自我心理调适方法演示文稿.ppt VIP
- 保险销售资质分级分类考试:2022投连险、变额年金保险销售资质考试真题模拟及答案(3).pdf VIP
- 现代工程图学习题集(第4版)杨裕根课后习题答案解析.docx
- 现代工程图学习题集(第5版)杨裕根 课后习题答案解析.pdf
- 2020新疆中考数学试题.pdf VIP
- 厦门市2024-2025学年高一上学期期末考试 物理试卷(含答案解析).pdf
- 标准图集-22G101-3 混凝土结构施工图平面整体表示方法制图规则和构造详图-独立基础、条形基础、筏形基础、桩基础.pdf VIP
- 语文-湖南省益阳市2024年高三下学期普通高中期末质量检测试题和答案.docx VIP
- 2024水利工程工地施工标准化管理指南.docx VIP
- 网页设计与制作网页设计与制作.doc VIP
原创力文档

文档评论(0)