linux文件的读写操作

Opens a file for sharing. These are versions of _sopen and _wsopen with security enhancements as described inSecurity Enhancements in the CRT.

 
errno_t _sopen_s(
   int* pfh,
   const char *filename,
      int oflag,
   int shflag,
   int pmode
);
errno_t _wsopen_s(
   int* pfh,
   const wchar_t *filename,
   int oflag,
   int shflag,
   int pmode,
);
[out] pfh

The file handle, or -1 in the case of an error.

[in] filename

File name.

[in] oflag

Type of operations allowed.

[in] shflag

Type of sharing allowed.

[in] pmode

Permission setting.


Reads data from a file.

 
int _read(
   int fd,
   void *buffer,
   unsigned int count 
);

Collapse imageParameters

fd

File descriptor referring to the open file.

buffer

Storage location for data.

count

Maximum number of bytes.

这个read操作就是将打开的文件操作符进行读操作,读取count个字节放到缓冲区中。返回读入的字节个数。

Parameters


 Copy imageCopy Code
// crt_read.c
/* This program opens a file named crt_read.txt
 * and tries to read 60,000 bytes from
 * that file using _read. It then displays the
 * actual number of bytes read.
 */

#include <fcntl.h>      /* Needed only for _O_RDWR definition */
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <share.h>

char buffer[60000];

int main( void )
{
   int fh;
   unsigned int nbytes = 60000, bytesread;

   /* Open file for input: */
   if( _sopen_s( &fh, "crt_read.txt", _O_RDONLY, _SH_DENYNO, 0 ) )
   {
      perror( "open failed on input file" );
      exit( 1 );
   }

   /* Read in input: */
   if( ( bytesread = _read( fh, buffer, nbytes ) ) <= 0 )
      perror( "Problem reading file" );
   else
      printf( "Read %u bytes from file\n", bytesread );

   _close( fh );
}

Writes data to a file.

 
int _write(
   int fd,
   const void *buffer,
   unsigned int count 
);

Collapse imageParameters

fd

File descriptor of file into which data is written.

buffer

Data to be written.

count

Number of bytes.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值