首页技术c语言write函数?C语言程序函数

c语言write函数?C语言程序函数

编程之家2026-06-281129次浏览

大家好,今天来为大家解答c语言write函数这个问题的一些问题点,包括C语言程序函数也一样很多人还不知道,因此呢,今天就来为大家分析分析,现在让我们一起来看看吧!如果解决了您的问题,还望您关注下本站哦,谢谢~

c语言write函数?C语言程序函数

C语言中的read和write怎么用

1.纠正:

read和write是UNIX或者一些类UNIX系统,比如LINUX系统中使用的,称为LINUX系统函数。这种函数只能在特定的操作系统下使用,可移植性差。

fread和fwrite是C库函数。这种函数基本在任何操作系统都能使用,可移植性高。

2.基础知识介绍

只介绍LINUX系统函数,常用的有creat,open,close,read,write,lseek,access,一般用于文件编程

3.如何使用

c语言write函数?C语言程序函数

谈到如何使用就必须说到另一个知识,文件描述符(file

description),是一个非负数。

函数原型:

int

read(int

fd,

c语言write函数?C语言程序函数

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、要用到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<sys\stat.h>

#include<io.h>

#include<string.h>

intmain(void)

{

int*handle;charstring[40];

intlength,res;/*Createafilenamed"TEST.$$$"inthecurrentdirectoryandwriteastringtoit.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语言编写的文件操作函数

这是因为:file=fopen("/tmp/test/test","a+");

模式“a+”中的 a规定以只写打开,【原文件数据】保留。

因此你不能读,只能写。写的时候只能在【原文件数据】的末尾开始写。

即使你将【文件位置指针】重新定位过,

在执行写操作的时候,

【文件位置指针】【自动】回到【原文件数据】的末尾了。

也就是【原文件数据】被保护起来,你是无法操作的。

【原文件数据】无法操作的定义是:不能再对【原文件数据】执行添加,删除,插入等。

因此,你要在【原文件数据】的开头插入数据,可能要绕个弯子:

1.用只读模式("a")先读出【原文件数据】,保存到内存

2.用只写模式(“w”)清除【原文件数据】

3.文件开头插入的信息。

4.再追加【原文件数据】(保存在内存)

当然如果在文件末尾添加就方便多了,用"a"模式一步到位。

下面使用这种思路进行插入信息的示例代码:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

const int MAX_FILE_SIZE= 100;

//读取文件内容

int fileRead(char fileName[],char content[],int* content_len)

{

FILE*infile;

char ch;

infile=fopen(fileName,"r");

*content_len=0;

if(infile==NULL)

{

printf("get infile eroor\n");

return 0;

}

else

{

rewind(infile);

while(EOF!=(ch=fgetc(infile)))

{

content[(*content_len)++]=ch;

}

}

fclose(infile);

return 1;

}

//在文件末尾追加内容

int fileAppend(char fileName[],char content[],int content_len)

{

FILE*outfile;

outfile=fopen(fileName,"a");

fwrite(content,content_len,1,outfile);

fclose(outfile);

}

int fileClear(char fileName[])

{

FILE*file= fopen(fileName,"w");

fclose(file);

}

//在文件开头插入内容

int insetMessageInfile(char fileName[],char message[],int message_len)

{

char content[MAX_FILE_SIZE];

int content_len=0;

fileRead(fileName,content,&content_len);

fileClear(fileName);

fileAppend(fileName,message,strlen(message));

fileAppend(fileName,content,content_len);

}

int main(void){

char fileName[]="data.txt";

char msg[3]="12";

//文件内容缓存区

char content[MAX_FILE_SIZE];

int content_len=0;

int i;

fileRead(fileName,content,&content_len);

for(i=0;i<content_len;i++) printf("%c",content[i]);

printf("\n");

insetMessageInfile("data.txt",msg,strlen(msg));

fileRead(fileName,content,&content_len);

for(i=0;i<content_len;i++) printf("%c",content[i]);

printf("\n");

return EXIT_SUCCESS;

}

好了,关于c语言write函数和C语言程序函数的问题到这里结束啦,希望可以解决您的问题哈!

cad视频教程全集下载(cad软件下载安装)vlookup提取多行?vlookup多行批量匹配