❶ c语言中的Write函数
write()写文件函数
原形:int
write(int
handle,char
*buf,unsigned
len)
用法:write(文件句柄,缓冲区地址,缓冲区字节长度<最大65534>);
功能:将缓冲区的数据写入与handle相联的文件或设备中,handle是从creat、open、p或p2调用中得到的文件句柄。对于磁盘或磁盘文件,写操作从当前文件指针处开始,对于用O_APPEND选项打开的文件,写数据之前,文件指针指向EOF;对于设备,字节被直接传送到设备中;
返回值:实际写入的字节数(不包括回车符),出错时返回-1。
头文件:io.h
❷ write写出常见的输入格式有
write写出常见的输入格式为f.write()。write函数是C语言函数。C语言函数是一种函数,用来编译C语言,所在库函数为ctype.h,分为分类函数,数学函数,目录函数,进程函数,诊断函数,操作函数等。
❸ C语言中的Write函数
write()写文件函数
原形:int write(int handle,char *buf,unsigned len)
功能:将缓冲区的数据写入与handle相联的文件或设备中,handle是从creat、open、p或p2调用中得到的文件句柄。
对于磁盘或磁盘文件,写操作从当前文件指针处开始,对于用O_APPEND选项打开的文件,写数据之前,文件指针指向EOF;对于设备,字节被直接传送到设备中。
(3)c语言write函数扩展阅读:
用法
头文件:<unistd.h>
write有两种用法。一种是:ssize_t write(int fd, const void *buf, size_t nbyte);
fd:文件描述符;
buf:指定的缓冲区,即指针,指向一段内存单元;
nbyte:要写入文件指定的字节数;
返回值:写入文档的字节数(成功);-1(出错)
write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.
另一种是: write(const char* str,int n)
str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
write("string",strlen("string");表示输出字符串常量
❹ C语言中的writef函数怎么用
函数名: 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;
}
❺ 在C语言中要用到write和read函数要用到什么头文件
1、要用到unistd.h头文件。
2、 Write函数
用法:
write函数所在的头文件为 <unistd.h>
write有两种用法。一种是:
ssize_twrite(int handle, void *buf, int nbyte);
handle 是文件描述符;
buf是指定的缓冲区,即指针,指向一段内存单元;
nbyte是要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)
write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.
另一种是:write(const char* str,int n)
str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
write("string",strlen("string");表示输出字符串常量
3、程序示例:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sysstat.h>
#include<io.h>
#include<string.h>
intmain(void)
{
int*handle;charstring[40];
intlength,res;/*Createafilenamed"TEST.$$$".If"TEST.$$$"alreadyexists,itwillbeoverwritten.*/
if((handle=open("TEST.$$$",O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE))==-1)
{
printf("Erroropeningfile. ");
exit(1);
}
strcpy(string,"Hello,world! ");
length=strlen(string);
if((res=write(handle,string,length))!=length)
{
printf("Errorwritingtothefile. ");
exit(1);
}
printf("Wrote%dbytestothefile. ",res);
close(handle);return0;}
❻ C语言中的read和write怎么用
1、read和write是UNIX或者一些类UNIX系统,比如LINUX系统中使用的,称为LINUX系统函数。这种函数只能在特定的操作系统下使用,可移植性差。fread和fwrite是C库函数。这种函数基本在任何操作系统都能使用,可移植性高。
2、基础知识介绍只介绍LINUX系统函数,常用的有creat,open,close,read,write,lseek,access,一般用于文件编程
3、如何使用谈到如何使用就必须说到另一个知识,文件描述符(file
description),是一个非负数。
函数原型:int
read(int
fd,
const
void
*buf,
size_t
length)
功能:
从文件描述符fd所指向的文件中读取length个字节到buf所指向的缓存区中,返回值为实际读取的字节数
int
write(int
fd,
const
void
*buf,
size_t
length)
功能:
把length个字节从buf所指向的缓存区中写到件描述符fd所指向的文件中,返回值为实际写入的字节数
例子:
#define
LENGTH
1024
#define
BUFFES_SIZE
1024
int
n1,
n2;
int
fd1,
fd2;
int
buffer[BUFFES_SIZE];fd1
=
open(
"HEllo1.txt",
O_RDWR
|
O_CREAT,
O_IRUSE
|
O_IWUSR);
fd2
=
open(
"HEllo2.txt",
O_RDWR
|
O_CREAT,
O_IRUSE
|
O_IWUSR);
n1
=
read(
fd1,
buffer,
LENGTH);
n2
=
write(
fd2,
buffer,
n1);
❼ C语言write函数的返回值问题
大多数情况下,write成功后返回的写入字节数都等于你传入的长度。
但是如果你要写的长度超过了的文件的最大可能时,比方说,你的磁盘还剩下128个字节,这时你向磁盘上的某个文件一次性写512个字节,返回值就是128,只有前128个字节成功写入。
再比如,你用write写的不是一个普通文件,而是设备文件/socket等,那也可能返回值小于你指定的值,这就可能是具体设备的限制等,比如写入的数量超过了缓冲大小等。
❽ c语言 write()、read()函数原型
把指定数量的数据写入文件,若成功则返回写入的字节数,否则返回-1
int _write( int handle, const void *buffer, unsigned int count );
这个例子中把字符串“hello”写入文件sample.txt中:
int Handle; char String[ ]="hello";
Handle = _open("sample.txt",_O_RDWR|_O_CREAT);
_write(Handle,String,sizeof(String));
_close(Handle);
从一个文件读取数据
int _read( int handle, void *buffer, unsigned int count );
这个例子中打开文件eof.c,每次读取10个字节,直到全部字节被读完为止,然后显示文件的长度:
void main( void ) {
int fh, count, total = 0;
char buf[10];
if( (fh = _open( "eof.c", _O_RDONLY )) == - 1 ) {
perror( "Open failed");
exit( 1 );
}
while( !_eof( fh ) ) {
if( (count = _read( fh, buf, 10 )) == -1 ) {
perror( "Read error" );
break;
}
total += count;
}
printf( "Number of bytes read = %d\n", total );
_close( fh );
}
❾ C语言中的read和write怎么用
1.纠正:
read和write是UNIX或者一些类UNIX系统,比如LINUX系统中使用的,称为LINUX系统函数。这种函数只能在特定的操作系统下使用,可移植性差。
fread和fwrite是C库函数。这种函数基本在任何操作系统都能使用,可移植性高。
2.基础知识介绍
只介绍LINUX系统函数,常用的有creat,open,close,read,write,lseek,access,一般用于文件编程
3.如何使用
谈到如何使用就必须说到另一个知识,文件描述符(file
description),是一个非负数。
函数原型:
int
read(int
fd,
const
void
*buf,
size_t
length)
功能:
从文件描述符fd所指向的文件中读取length个字节到buf所指向的缓存区中,返回值为实际读取的字节数
int
write(int
fd,
const
void
*buf,
size_t
length)
功能:
把length个字节从buf所指向的缓存区中写到件描述符fd所指向的文件中,返回值为实际写入的字节数
例子:
#define
LENGTH
1024
#define BUFFES_SIZE
1024
int
n1,
n2;
int
fd1,
fd2;
int
buffer[BUFFES_SIZE];
fd1
=
open(
"HEllo1.txt",
O_RDWR
|
O_CREAT,
O_IRUSE
|
O_IWUSR);
fd2
=
open(
"HEllo2.txt",
O_RDWR
|
O_CREAT,
O_IRUSE
|
O_IWUSR);
n1 =
read(
fd1,
buffer, LENGTH);
n2 =
write(
fd2,
buffer, n1);
好了累死了,答案完全原创,希望对你有帮助
❿ C语言 write和read语句的基本用法
1、函数名: write
表头文件:#include<unistd.h>
定义函数:ssize_t write (int fd,const void * buf,size_t count);
函数说明:write()会把指针buf所指的内存写入count个字节到参数fd所指的文件内。当然,文件读写位置也会随之移动。
返回值:如果顺利write()会返回实际写入的字节数。当有错误发生时则返回-1,错误代码存入errno中。
错误代码:
EINTR 此调用被信号所中断。
EAGAIN 当使用不可阻断I/O 时(O_NONBLOCK),若无数据可读取则返回此值。
EBADF 参数fd非有效的文件描述词,或该文件已关闭。
程序例:
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<errno.h>
intmain(void)
{
inthandle;
charstring[40];
intlength,res;
/*
Createafilenamed"TEST.$$$"inthecurrentdirectoryandwrite
astringtoit.If"TEST.$$$"alreadyexists,itwillbeoverwritten.
*/
if((handle=open("TEST.$$$",O_WRONLY|O_CREAT|O_TRUNC,
S_IREAD|S_IWRITE))==-1)
{
printf("Erroropeningfile. ");
exit(1);
}
strcpy(string,"Hello,world! ");
length=strlen(string);
if((res=write(handle,string,length))!=length)
{
printf("Errorwritingtothefile. ");
exit(1);
}
printf("Wrote%dbytestothefile. ",res);
close(handle);
return0;
}
structxfcb{
charxfcb_flag;/*Contains0xfftoindicatexfcb*/
charxfcb_resv[5];/*ReservedforDOS*/
charxfcb_attr;/*Searchattribute*/
structfcbxfcb_fcb;/*Thestandardfcb*/
};
2、函数名: read
表头文件:#include<unistd.h>
定义函数:ssize_t read(int fd,void * buf ,size_t count);
函数说明:read()会把参数fd 所指的文件传送count个字节到buf指针所指的内存中。若参数count为0,则read为实际读取到的字节数,如果返回0,表示已到达文件尾或是无可读取的数据,此外文件读写位置会随读取到的字节移动。
附加说明:如果顺利read()会返回实际读到的字节数,最好能将返回值与参数count 作比较,若返回的字节数比要求读取的字节数少,则有可能读到了文件尾、从管道(pipe)或终端机读取,或者是read()被信号中断了读取动作。当有错误发生时则返回-1,错误代码存入errno中,而文件读写位置则无法预期。
错误代码:
EINTR 此调用被信号所中断。
EAGAIN 当使用不可阻断I/O 时(O_NONBLOCK),若无数据可读取则返回此值。
EBADF 参数fd 非有效的文件描述词,或该文件已关闭。
程序例:
#include
#include
#include
#include
#include
#include
intmain(void)
{
void*buf;
inthandle,bytes;
buf=malloc(10);
/*
.$$$andattempts
toread10bytesfromit.To
}
if((bytes=read(handle,buf,10))==-1){
printf("ReadFailed. ");
exit(1);
}
else{
printf("Read:%dbytesread. ",bytes);
}
return0;