依睛(IT blog) 我回来了,PHP<-->C/C++ LINUX

笨鸟

统计

积分与排名

友情连接

最新评论

fscanf和fprintf 的详细用法

fscanf和fprintf 的详细用法 [已结贴,结贴人:sure2003]
进入用户个人空间
加为好友
发送私信
在线聊天
  • sure2003
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
  • 揭帖率:
发表于:2007-12-17 22:41:39 楼主
fread
fwrite
书上说只能用于读二进制文件,可我也能编出读文本文件
fcanf和fprintf
的用法,谭哥书上没说,希望大家介绍的详细一点
50  修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • Minkey
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-17 22:49:46 1楼 得分:4
楼主可真够大手笔的啊...
这种问题百度一下一大堆的...呵呵
看看这个吧:
http://blog.csdn.net/iu_81/archive/2007/03/27/1542735.aspx
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • Minkey
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-17 22:52:36 2楼 得分:1
晕,看错了,那个链接讲的是fread和fwrite...
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • lockhall
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-17 22:58:08 3楼 得分:8
举个例子,楼主看下吧.

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
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • PcrazyC
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-17 22:58:08 4楼 得分:8
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 ; }


C/C++ code
																								
函数名: fwrite 功 能: 写内容到流中 用 法: int fwrite( void * ptr, int size, int nitems, FILE * stream); 程序例: #include < stdio.h > struct mystruct { int i; char ch; }; int main( void ) { FILE * stream; struct mystruct s; if ((stream = fopen( " TEST.$$$ " , " wb " )) == NULL) /* open file TEST.$$$ */ { fprintf(stderr, " Cannot open output file.\n " ); return 1 ; } s.i = 0 ; s.ch = ' A ' ; fwrite( & s, sizeof (s), 1 , stream); /* write struct s to file */ fclose(stream); /* close file */ return 0 ; }
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • PcrazyC
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-17 22:58:40 5楼 得分:1
晕死,受1楼的影响
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • PcrazyC
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-17 22:59:18 6楼 得分:6
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 ; }



C/C++ code
																								
函数名: fprintf 功 能: 传送格式化输出到一个流中 用 法: int fprintf(FILE * stream, char * format[, argument,...]); 程序例: /* Program to create backup of the AUTOEXEC.BAT file */ #include < stdio.h > int main( void ) { FILE * in , * out ; if (( in = fopen( " \\AUTOEXEC.BAT " , " rt " )) == NULL) { fprintf(stderr, " Cannot open input \ file.\n " ); return 1 ; } if (( out = fopen( " \\AUTOEXEC.BAK " , " wt " )) == NULL) { fprintf(stderr, " Cannot open output \ file.\n " ); return 1 ; } while ( ! feof( in )) fputc(fgetc( in ), out ); fclose( in ); fclose( out ); return 0 ; }
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • jixingzhong
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
  • 2

    17

    2

发表于:2007-12-18 08:17:08 7楼 得分:4
fscanf  sscanf 和 scanf 工作原理是一致的,
区别在于输入方向:
scanf 从控制台输入
fscanf 从文件输入
sscanf 从指定字符串输入

建议看看
http://blog.csdn.net/jixingzhong/archive/2007/01/07/1476089.aspx
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • jixingzhong
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
  • 2

    17

    2

发表于:2007-12-18 08:18:58 8楼 得分:5
fread fwrite 最好是用于操作 二进制方式打开的文件

当然,如果一定要用来操作文本文件,
有时候结果也是正确的。
但是这个 结果正确 是无法得到保证的,
也就是说,当使用fread fwrite读写文本文件时候,可能会出现错误,比如操作字符数不正确,等等
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • zhulinpptor
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-18 08:19:53 9楼 得分:10
scanf  从控制台输入
fscanf  从文件输入
sscanf  从指定字符串输入
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • ztz0223
  • 等级:
  • 可用分等级:
  • 总技术专家分:
  • 总技术专家分排名:
发表于:2007-12-18 08:25:05 10楼 得分:0
程序已经有人给你了
我给你一个msdn上的说法算了:

fread:
C/C++ code
Reads data from a stream. size_t fread( void*buffer, size_t size, size_t count, FILE *stream ); Parameters buffer Storage location for data. size Item size in bytes. count Maximum number of items to be read. stream Pointer to FILE structure. Return Value fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is0, fread returns 0 and the buffer contents are unchanged. If stream or buffer is a null pointer, fread invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0. See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes. Remarks The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined. This function locks out other threads. If you need a non-locking version, use _fread_nolock. Requirements Function Required header fread <stdio.h> For additional compatibility information, see Compatibility in the Introduction. Example Copy Code // crt_fread.c // This program opens a file named FREAD.OUT and // writes 25 characters to the file. It then tries to open // FREAD.OUT and read in 25 characters. If the attempt succeeds, // the program displays the number of actual items read. #include <stdio.h>int main( void ) { FILE *stream; char list[30]; int i, numread, numwritten; // Open file in text mode:if( fopen_s( &stream, "fread.out", "w+t" ) ==0 ) { for ( i =0; i <25; i++ ) list[i] = (char)('z'- i); // Write 25 characters to stream numwritten = fwrite( list, sizeof( char ), 25, stream ); printf( "Wrote %d items\n", numwritten ); fclose( stream ); } else printf( "Problem opening the file\n" ); if( fopen_s( &stream, "fread.out", "r+t" ) ==0 ) { // Attempt to read in 25 characters numread = fread( list, sizeof( char ), 25, stream ); printf( "Number of items read = %d\n", numread ); printf( "Contents of buffer = %.25s\n", list ); fclose( stream ); } else printf( "File could not be opened\n" ); } Copy Code Wrote 25 items Number of items read =25 Contents of buffer = zyxwvutsrqponmlkjihgfedcb



fprintf:

C/C++ code
int fprintf( FILE *stream, constchar*format [, argument ]... ); Parameters stream Pointer to FILE structure. format Format-control string. argument Optional arguments. locale The locale to use. Example Copy Code // crt_fprintf.c /* This program uses fprintf to format various * data and print it to the file named FPRINTF.OUT. It * then displays FPRINTF.OUT on the screen using the system * function to invoke the operating-system TYPE command. */ #include <stdio.h> #include <process.h> FILE *stream; int main( void ) { int i =10; double fp =1.5; char s[] ="this is a string"; char c ='\n'; fopen_s( &stream, "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" ); } Copy Code thisis a string101.500000

posted on 2009-01-17 17:30 向左向右走 阅读(19601) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。