C语言 write和read语句的基本用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 write和read语句的基本用法相关的知识,希望对你有一定的参考价值。

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.\\n");
exit(1);

 
strcpy(string,"Hello,world!\\n");
length=strlen(string);
 
if((res=write(handle,string,length))!=length)

printf("Errorwritingtothefile.\\n");
exit(1);

 
printf("Wrote%dbytestothefile.\\n",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
int main(void)

void *buf;
int handle, bytes;
buf = malloc(10);
/*
Looks for a file in the current directory named TEST.$$$ and attempts
to read 10 bytes from it. To

if ((bytes = read(handle, buf, 10)) == -1) 
printf("Read Failed.\\n");
exit(1);

else 
printf("Read: %d bytes read.\\n", bytes);

return 0;

  

参考技术A 你说的是pascal语言吧
c语言读入数据用是scanf或printf
c++还可以用cin或cout
scanf("数据类型",&要读入的变量);
printf("数据类型和格式",要输出的变量);
数据类型和变量定义的关键字有关
下面给几个例子
数据类型 变量定义的关键字
%d int
%ld long (或__int64)
%f float
%lf double
%c char(一个字符)
%s char(字符数组)
输出的时候有一些控制字符
/n 回车
/b 退格
输出小数时

printf("%.nf"s);(n为数字)
表示输出小数点后几位
还有对齐的输出
printf("%nf",s);(n为数字)
n小于0时左对齐
n大于0时右对齐
总共占n位
int t=23;
printf("%3d",t);
结果是 23(前面有一个空格)
int t=23;
printf("%-3d",t);
结果是23 (后面有一个空格)
参考技术B 函数名: read
功 能: 从文件中读
用 法: int read(int handle, void *buf, int nbyte);
程序例:

#include <stdio.h>
#include <io.h>
#include <alloc.h>
#include <fcntl.h>
#include <process.h>
#include <sys/stat.h>

int main(void)

void *buf;
int handle, bytes;

buf = malloc(10);

/*
Looks for a file in the current directory named TEST.$$$ and attempts
to read 10 bytes from it. To use this example you should create the
file TEST.$$$
*/
if ((handle =
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)

printf("Error Opening File/n");
exit(1);


if ((bytes = read(handle, buf, 10)) == -1)
printf("Read Failed./n");
exit(1);

else
printf("Read: %d bytes read./n", bytes);

return 0;


函数名: write
功 能: 写到一文件中
用 法: int write(int handel, void *buf, int nbyte);
程序例:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
#include <string.h>

int main(void)

int handle;
char string[40];
int length, res;

/*
Create a file named "TEST.$$$" in the current directory and write
a string to it. If "TEST.$$$" already exists, it will be overwritten.
*/

if ((handle = open("TEST.$$$", O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE)) == -1)

printf("Error opening file./n");
exit(1);


strcpy(string, "Hello, world!/n");
length = strlen(string);

if ((res = write(handle, string, length)) != length)

printf("Error writing to the file./n");
exit(1);

printf("Wrote %d bytes to the file./n", res);

close(handle);
return 0;

参考资料:http://www.fzs8.net/C_Function/index_7.html

以上是关于C语言 write和read语句的基本用法的主要内容,如果未能解决你的问题,请参考以下文章

c语言中if else的用法

c语言if语句的用法

C语言07-基本语句和运算

关于C语言中if else的用法,高手来帮下忙

Flutter入门Dart语言:控制流语句的基本用法

C *&的用法