网站大量收购独家精品文档,联系QQ:2885784924

fprintf_fscan讲述.docx

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
fprintf_fscan讲述

对文件的读和写是最常用的文件操作。在C语言中提供了多种文件读写的函数: ·字符读写函数 :fgetc和fputc ·字符串读写函数:fgets和fputs ·数据块读写函数:fread和fwrite ·格式化读写函数:fscanf和fprinf 随着每次数据的读取,文件流指针fp都作相应的移动 使用以上函数都要求包含头文件stdio.h。例子都来自msdn 1 fprintf——Print formatted data to a stream #include stdio.h #include process.h FILE *stream; void main( void ) { ? int??? i = 10; ? double fp = 1.5; ? char?? s[] = this is a string; ? char?? c = \n; ? stream = fopen( fprintf.out, w ); ? fprintf( stream, %s%c, s, c ); ? fprintf( stream, %d\n, i ); ? fprintf( stream, %f\n, fp ); ? fclose( stream ); ? system( type fprintf.out ); } Output this is a string 10 1.500000 2 fscanf——Read formatted data from a stream #include stdio.h FILE *stream; void main( void ) { ? long l; ? float fp; ? char s[81]; ? char c; ? stream = fopen( fscanf.out, w+ ); ? if( stream == NULL ) ???? printf( The file fscanf.out was not opened\n ); ? else ? { ???? fprintf( stream, %s %ld %f %c, a-string, ????????????? 65000, 3.14159, x ); ???? /* Set pointer to beginning of file: */ ???? fseek( stream, 0L, SEEK_SET ); ???? /* Read data back from file: */ ???? fscanf( stream, %s, s ); ???? fscanf( stream, %ld, l ); ???? fscanf( stream, %f, fp ); ???? fscanf( stream, %c, c ); ???? /* Output data read: */ ???? printf( %s/n, s ); ???? printf( %ld/n, l ); ???? printf( %f/n, fp ); ???? printf( %c/n, c ); ???? fclose( stream ); ? } } Output a-string 65000 3.141590 x 3 fread——Reads data from a stream 4 fwrite——Writes data to a stream 读数据块函数调用的一般形式为: fread(buffer,size,count,fp); 写数据块函数调用的一般形式为: fwrite(buffer,size,count,fp); 其中: buffer 是一个指针,在fread函数中,它表示存放输入数据的首地址。在fwrite函数中,它表示存放输出数据的首地址。 size 表示数据块的字节数。 count 表示要读??的数据块块数。 fp 表示文件指针。 5 fgets 没有看出与fread太大的区别,除了fread可以处理string外的其他不同文件的数据类型 6 fputs 7 fgetc fputs 从键盘输入一行字符,写入一个文件,再把该文件内容读出显示在屏幕上。 #i nclude main() { FILE *fp; char ch; if((fp=fopen(d://jrzh//example//string,wt+))==NULL) { printf(Cannot open file strike any key exit!); getch(); exit(1); } printf(input a string:\n);

文档评论(0)

jiayou10 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8133070117000003

1亿VIP精品文档

相关文档