登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

聊天机器

Chatbot's private blog

 
 
 

日志

 
 

对于网上一堆解析fwrite参数感到极度郁闷  

2009-07-17 11:43:49|  分类: 程序理论 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

太多人不懂装懂了。。。

以下是MSDN的解析,不懂英文或者不想看英文的看后面
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined.

Parameters

buffer

Pointer to data to be written

size

Item size in bytes

count

Maximum number of items to be written

stream

Pointer to FILE structure

Remarks

The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. If stream is opened in text mode, each carriage return is replaced with a carriage-return – linefeed pair. The replacement has no effect on the return value.

Example

/* 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>

void main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;

   /* Open file in text mode: */
   if( (stream = fopen( "fread.out", "w+t" )) != NULL )
   {
      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( (stream = fopen( "fread.out", "r+t" )) != NULL )
   {
      /* 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" );
}


Output

Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb

 

中文部分:
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
buffer
    容易理解,对于要写入的变量的指针,假如是变量或者对象,用&就可以了。假如我要把一个int写入文件
那么
int a = 0;
fwrite( &a, sizeof(int), 1, pFile );就可以了


size
    看上面的例子就可以知道,其实是关于你要写的结构的占用内存大小。例如变量为char,则sizeof(char),某些结构,就sizeof(*)。对于网上对于此参数解析我觉得极度郁闷。。。怎么可以有这些不负责任的人呢?(注意,对于有指针的结构,sizeof(指针)= 4,假如不理解请复习指针。。。)

count
    数量,
    int a = 0;
    fwrite( &a, sizeof(int), 1, pFile );
    这里由于变量只有1个,则使用1,其实,这个是从第一个参数提供的地址开始,以第二个参数为单位读取数据的数量。当然了,由于储存方式的关系,通常只有数组才可以这么做,其它的。。。还是不能偷懒~

stream
    流,使用fopen的返回值。这里看fopen的MSDN更好~~~~

返回值:返回成功写了多大~~~

  评论这张
 
阅读(538)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018