code writer c语言,寻 c语言函数fwrite和fread的源代码?

这是fwrite的源文件

/***

*fwrite.c - read from a stream

*

* Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.

*

*Purpose:

* Write to the specified stream from the user's buffer.

*

*******************************************************************************/

#include

#include

#include

#include

#include

#include

#include

/***

*size_t fwrite(void *buffer, size_t size, size_t count, FILE *stream) -

* write to the specified stream from the specified buffer.

*

*Purpose:

* Write 'count' items of size 'size' to the specified stream from

* the specified buffer. Return when 'count' items have been written

* or no more items can be written to the stream.

*

*Entry:

* buffer - pointer to user's buffer

* size - size of the item to write

* count - number of items to write

* stream - stream to write to

*

*Exit:

* Returns the number of (whole) items that were written to the stream.

* This may be less than 'count' if an error or eof occurred. In this

* case, ferror() or feof() should be used to distinguish between the

* two conditions.

*

*Notes:

* fwrite will attempt to buffer the stream (side effect of the _flsbuf

* call) if necessary.

*

* No more than 0xFFFE bytes may be written out at a time by a call to

* write(). Further, write() does not handle huge buffers. Therefore,

* in large data models, the write request is broken down into chunks

* that do not violate these considerations. Each of these chunks is

* processed much like an fwrite() call in a small data model (by a

* call to _nfwrite()).

*

* This code depends on _iob[] being a near array.

*

* MTHREAD/DLL - Handled in just two layers since it is small data

* model. The outer layer, fwrite(), handles stream locking/unlocking

* and calls _fwrite_lk() to do the work. _fwrite_lk() is the same as

* the single-thread, small data model version of fwrite().

*

*******************************************************************************/

#ifdef _MT

/* define locking/unlocking version */

size_t __cdecl fwrite (

const void *buffer,

size_t size,

size_t count,

FILE *stream

)

{

size_t retval;

_lock_str(stream); /* lock stream */

retval = _fwrite_lk(buffer, size, count, stream); /* do the read */

_unlock_str(stream); /* unlock stream */

return retval;

}

#endif /* _MT */

/* define the normal version */

#ifdef _MT

size_t __cdecl _fwrite_lk (

#else /* _MT */

size_t __cdecl fwrite (

#endif /* _MT */

const void *buffer,

size_t size,

size_t num,

FILE *stream

)

{

const char *data; /* point to where data comes from next */

unsigned total; /* total bytes to write */

unsigned count; /* num bytes left to write */

unsigned bufsize; /* size of stream buffer */

unsigned nbytes; /* number of bytes to write now */

unsigned nwritten; /* number of bytes written */

int c; /* a temp char */

/* initialize local vars */

data = buffer;

count = total = size * num;

if (0 == count)

return 0;

if (anybuf(stream))

/* already has buffer, use its size */

bufsize = stream->_bufsiz;

else

#if defined (_M_M68K) || defined (_M_MPPC)

/* assume will get BUFSIZ buffer */

bufsize = BUFSIZ;

#else /* defined (_M_M68K) || defined (_M_MPPC) */

/* assume will get _INTERNAL_BUFSIZ buffer */

bufsize = _INTERNAL_BUFSIZ;

#endif /* defined (_M_M68K) || defined (_M_MPPC) */

/* here is the main loop -- we go through here until we're done */

while (count != 0) {

/* if the buffer is big and has room, copy data to buffer */

if (bigbuf(stream) && stream->_cnt != 0) {

/* how much do we want? */

nbytes = (count < (unsigned)stream->_cnt) ? count : stream->_cnt;

memcpy(stream->_ptr, data, nbytes);

/* update stream and amt of data written */

count -= nbytes;

stream->_cnt -= nbytes;

stream->_ptr += nbytes;

data += nbytes;

}

else if (count >= bufsize) {

/* If we have more than bufsize chars to write, write

data by calling write with an integral number of

bufsiz blocks. If we reach here and we have a big

buffer, it must be full so _flush it. */

if (bigbuf(stream)) {

if (_flush(stream)) {

/* error, stream flags set -- we're out

of here */

return (total - count) / size;

}

}

/* calc chars to read -- (count/bufsize) * bufsize */

nbytes = ( bufsize ? (count - count % bufsize) :

count );

nwritten = _write(_fileno(stream), data, nbytes);

if (nwritten == (unsigned)EOF) {

/* error -- out of here */

stream->_flag |= _IOERR;

return (total - count) / size;

}

/* update count and data to reflect write */

count -= nwritten;

data += nwritten;

if (nwritten < nbytes) {

/* error -- out of here */

stream->_flag |= _IOERR;

return (total - count) / size;

}

}

else {

/* buffer full and not enough chars to do direct write,

so do a _flsbuf. */

c = *data; /* _flsbuf write one char, this is it */

if (_flsbuf(c, stream) == EOF) {

/* error or eof, stream flags set by _flsbuf */

return (total - count) / size;

}

/* _flsbuf wrote a char -- update count */

++data;

--count;

/* update buffer size */

bufsize = stream->_bufsiz > 0 ? stream->_bufsiz : 1;

}

}

/* we finished successfully, so just return num */

return num;

}

这是fread的源文件

/***

*fread.c - read from a stream

*

* Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.

*

*Purpose:

* Read from the specified stream into the user's buffer.

*

*******************************************************************************/

#include

#include

#include

#include

#include

#include

/***

*size_t fread(void *buffer, size_t size, size_t count, FILE *stream) -

* read from specified stream into the specified buffer.

*

*Purpose:

* Read 'count' items of size 'size' from the specified stream into

* the specified buffer. Return when 'count' items have been read in

* or no more items can be read from the stream.

*

*Entry:

* buffer - pointer to user's buffer

* size - size of the item to read in

* count - number of items to read

* stream - stream to read from

*

*Exit:

* Returns the number of (whole) items that were read into the buffer.

* This may be less than 'count' if an error or eof occurred. In this

* case, ferror() or feof() should be used to distinguish between the

* two conditions.

*

*Notes:

* fread will attempt to buffer the stream (side effect of the _filbuf

* call) if necessary.

*

* No more than 0xFFFE bytes may be read in at a time by a call to

* read(). Further, read() does not handle huge buffers. Therefore,

* in large data models, the read request is broken down into chunks

* that do not violate these considerations. Each of these chunks is

* processed much like an fread() call in a small data model (by a

* call to _nfread()).

*

* MTHREAD/DLL - Handled in three layers. fread() handles the locking

* and DS saving/loading/restoring (if required) and calls _fread_lk()

* to do the work. _fread_lk() is the same as the single-thread,

* large data model version of fread(). It breaks up the read request

* into digestible chunks and calls _nfread() to do the actual work.

*

* 386/MTHREAD/DLL - Handled in just the two layers since it is small

* data model. The outer layer, fread(), takes care of the stream locking

* and calls _fread_lk() to do the actual work. _fread_lk() is the same

* as the single-thread version of fread().

*

*******************************************************************************/

#ifdef _MT

/* define locking/unlocking version */

size_t __cdecl fread (

void *buffer,

size_t size,

size_t count,

FILE *stream

)

{

size_t retval;

_lock_str(stream); /* lock stream */

retval = _fread_lk(buffer, size, count, stream); /* do the read */

_unlock_str(stream); /* unlock stream */

return retval;

}

#endif /* _MT */

/* define the normal version */

#ifdef _MT

size_t __cdecl _fread_lk (

#else /* _MT */

size_t __cdecl fread (

#endif /* _MT */

void *buffer,

size_t size,

size_t num,

FILE *stream

)

{

char *data; /* point to where should be read next */

unsigned total; /* total bytes to read */

unsigned count; /* num bytes left to read */

unsigned bufsize; /* size of stream buffer */

unsigned nbytes; /* how much to read now */

unsigned nread; /* how much we did read */

int c; /* a temp char */

/* initialize local vars */

data = buffer;

if ( (count = total = size * num) == 0 )

return 0;

if (anybuf(stream))

/* already has buffer, use its size */

bufsize = stream->_bufsiz;

else

#if defined (_M_M68K) || defined (_M_MPPC)

/* assume will get BUFSIZ buffer */

bufsize = BUFSIZ;

#else /* defined (_M_M68K) || defined (_M_MPPC) */

/* assume will get _INTERNAL_BUFSIZ buffer */

bufsize = _INTERNAL_BUFSIZ;

#endif /* defined (_M_M68K) || defined (_M_MPPC) */

/* here is the main loop -- we go through here until we're done */

while (count != 0) {

/* if the buffer exists and has characters, copy them to user

buffer */

if (anybuf(stream) && stream->_cnt != 0) {

/* how much do we want? */

nbytes = (count < (unsigned)stream->_cnt) ? count : stream->_cnt;

memcpy(data, stream->_ptr, nbytes);

/* update stream and amt of data read */

count -= nbytes;

stream->_cnt -= nbytes;

stream->_ptr += nbytes;

data += nbytes;

}

else if (count >= bufsize) {

/* If we have more than bufsize chars to read, get data

by calling read with an integral number of bufsiz

blocks. Note that if the stream is text mode, read

will return less chars than we ordered. */

/* calc chars to read -- (count/bufsize) * bufsize */

nbytes = ( bufsize ? (count - count % bufsize) :

count );

nread = _read(_fileno(stream), data, nbytes);

if (nread == 0) {

/* end of file -- out of here */

stream->_flag |= _IOEOF;

return (total - count) / size;

}

else if (nread == (unsigned)-1) {

/* error -- out of here */

stream->_flag |= _IOERR;

return (total - count) / size;

}

/* update count and data to reflect read */

count -= nread;

data += nread;

}

else {

/* less than bufsize chars to read, so call _filbuf to

fill buffer */

if ((c = _filbuf(stream)) == EOF) {

/* error or eof, stream flags set by _filbuf */

return (total - count) / size;

}

/* _filbuf returned a char -- store it */

*data++ = (char) c;

--count;

/* update buffer size */

bufsize = stream->_bufsiz;

}

}

/* we finished successfully, so just return num */

return num;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
fwritefread函数C语言中用于文件读写的函数,它们都是标准库函数,定义在头文件stdio.h中。 fwrite函数的原型如下: ```c size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream); ``` 参数说明: - ptr:要写入的数据的指针。 - size:每个数据项的大小(单位为字节)。 - count:要写入的数据项的数量。 - stream:文件指针。 返回值为成功写入的数据项的数量。 fwrite函数用于将数据写入文件中,它按照指定的大小和数量将数据写入文件中。如果写入成功,函数返回成功写入的数据项的数量,否则返回0。 例如,下面的代码将一个整数数组写入文件中: ```c #include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; FILE *fp = fopen("data.txt", "wb"); if (fp == NULL) { printf("Failed to open file.\n"); return 1; } fwrite(arr, sizeof(int), 5, fp); fclose(fp); return 0; } ``` fread函数的原型如下: ```c size_t fread(void *ptr, size_t size, size_t count, FILE *stream); ``` 参数说明: - ptr:要读取数据的缓冲区的指针。 - size:每个数据项的大小(单位为字节)。 - count:要读取的数据项的数量。 - stream:文件指针。 返回值为实际读取的数据项的数量。 fread函数用于从文件中读取数据,它按照指定的大小和数量从文件中读取数据。如果读取成功,函数返回实际读取的数据项的数量,否则返回0。 例如,下面的代码从文件中读取一个整数数组: ```c #include <stdio.h> int main() { int arr[5]; FILE *fp = fopen("data.txt", "rb"); if (fp == NULL) { printf("Failed to open file.\n"); return 1; } fread(arr, sizeof(int), 5, fp); fclose(fp); for (int i = 0; i < 5; i++) { printf("%d\n", arr[i]); } return 0; } ``` 以上代码会输出以下结果: ``` 1 2 3 4 5 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值