#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int fd =open("a.txt",O_RDONLY);//读出文件内容
assert( fd != -1);
char buff[128]={0};
int n=read(fd ,buff ,127);
printf("buff=%s,n=%d\n",buff ,n);
close(fd);
/*
int fd=open("a.txt",O_WRONLY | O_CREAT , 0600);//写入内容
assert( fd != -1);
write(fd,"hello",5);
close(fd);
*/
}
~
linux文件操作
最新推荐文章于 2024-09-15 22:47:38 发布
该代码示例展示了如何使用C语言标准库函数打开、读取和关闭一个名为'a.txt'的文件。首先,它以只读模式打开文件,并读取前127个字符到缓冲区中,然后打印出来。注释掉的部分则演示了如何以写入模式向同一文件写入'hello'字符串。
摘要由CSDN通过智能技术生成