fscanf和fprintf 的详细用法解决方案
fscanf和fprintf 的详细用法
fread
fwrite
书上说只能用于读二进制文件,可我也能编出读文本文件
fcanf和fprintf
的用法,谭哥书上没说,希望大家介绍的详细一点
------解决方案--------------------
楼主可真够大手笔的啊...
这种问题百度一下一大堆的...呵呵
看看这个吧:
http://blog.****.net/iu_81/archive/2007/03/27/1542735.aspx
------解决方案--------------------
晕,看错了,那个链接讲的是fread和fwrite...
------解决方案--------------------
举个例子,楼主看下吧.
fprintf是用于文件操作的,原型是int fprintf( FILE *stream, const char *format [, argument ]...);
举例用法:
#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" );
}
屏幕输出:
this is a string
10
1.500000
------解决方案--------------------
fread
fwrite
书上说只能用于读二进制文件,可我也能编出读文本文件
fcanf和fprintf
的用法,谭哥书上没说,希望大家介绍的详细一点
------解决方案--------------------
楼主可真够大手笔的啊...
这种问题百度一下一大堆的...呵呵
看看这个吧:
http://blog.****.net/iu_81/archive/2007/03/27/1542735.aspx
------解决方案--------------------
晕,看错了,那个链接讲的是fread和fwrite...
------解决方案--------------------
举个例子,楼主看下吧.
fprintf是用于文件操作的,原型是int fprintf( FILE *stream, const char *format [, argument ]...);
举例用法:
#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" );
}
屏幕输出:
this is a string
10
1.500000
------解决方案--------------------
- C/C++ code
函数名: fread 功 能: 从一个流中读数据 用 法: int fread(void *ptr, int size, int nitems, FILE *stream); 程序例: #include <string.h> #include <stdio.h> int main(void) { FILE *stream; char msg[] = "this is a test"; char buf[20]; if ((stream = fopen("DUMMY.FIL", "w+")) == NULL) { fprintf(stderr, "Cannot open output file.\n"); return 1; } /* write some data to the file */ fwrite(msg, strlen(msg)+1, 1, stream); /* seek to the beginning of the file */ fseek(stream, SEEK_SET, 0); /* read the data and display it */ fread(buf, strlen(msg)+1, 1, stream); printf("%s\n", buf); fclose(stream); return 0; }
------解决方案--------------------
晕死,受1楼的影响
------解决方案--------------------
- C/C++ code
函数名: fscanf 功 能: 从一个流中执行格式化输入 用 法: int fscanf(FILE *stream, char *format[,argument...]); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { int i; printf("Input an integer: "); /* read an integer from the standard input stream */ if (fscanf(stdin, "%d", &i)) printf("The integer read was: %i\n", i); else { fprintf(stderr, "Error reading an \ integer from stdin.\n"); exit(1); } return 0; }
------解决方案--------------------
fscanf sscanf 和 scanf 工作原理是一致的,
区别在于输入方向:
scanf 从控制台输入
fscanf 从文件输入
sscanf 从指定字符串输入
建议看看
http://blog.****.net/jixingzhong/archive/2007/01/07/1476089.aspx
------解决方案--------------------
fread fwrite 最好是用于操作 二进制方式打开的文件
当然,如果一定要用来操作文本文件,
有时候结果也是正确的。
但是这个 结果正确 是无法得到保证的,
也就是说,当使用fread fwrite读写文本文件时候,可能会出现错误,比如操作字符数不正确,等等
------解决方案--------------------
scanf 从控制台输入
fscanf 从文件输入
sscanf 从指定字符串输入
------解决方案--------------------
中国英语写的不错
------解决方案--------------------