当前位置: 首页 > news >正文

Linux程序设计:文件操作

文件操作

系统调用

write

//函数定义
#include <unistd.h>
size_t write(int fildes, const void *buf, size_t nbytes);
//示例程序
#include <unistd.h>
#include <stdlib.h>
int main()
{
if ((write(1, “Here is some data\n”, 18)) != 18)write(2, “A write error has occurred on file descriptor 1\n”,46);
exit(0);
}

read

//函数定义
#include <unistd.h>
size_t read(int fildes, void *buf, size_t nbytes);
//示例程序
#include <unistd.h>
#include <stdlib.h>
int main()
{char buffer[128];int nread;nread = read(0, buffer, 128);//打开文件后,0代表标准输入,1代表标准输出,2代表错误输出if (nread == -1)write(2, “A read error has occurred\n”, 26);if ((write(1,buffer,nread)) != nread)write(2, “A write error has occurred\n”,27);exit(0);
}

重定向,输入重定向,管道功能

$	echo hello there | simple_read
#   echo hello there 会将字符串 "hello there" 输出到终端上。
#   | 是管道符,它将前面输出的内容作为后面命令的输入。
#   simple_read 是一个命令行程序,它会从标准输入中读取一行文本,并将其打印到标准输出上。
#	 将字符串 "hello there" 通过管道传递给 simple_read,simple_read 会将其读取并打印到终端上。
hello there$	simple_read < draft1.txt	#将文件" draft1.txt "的内容显示在终端
Files
In this chapter we will be looking at files and directories and how to manipulate
them. We will learn how to create files, o$

open

//函数定义
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);open (“myfile”, O_CREAT, S_IRUSR|S_IXOTH);

close

//函数定义
#include <unistd.h>
int close(int fildes);
//应用示例:文件复制的程序
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>int main()
{char c;int in, out;in = open(“file.in”, O_RDONLY);out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);while(read(in,&c,1) == 1)write(out,&c,1);exit(0);
}

lseek

//函数定义:确定文件的读写位置
#include <unistd.h>
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);

❑ SEEK_SET: offset is an absolute position

❑ SEEK_CUR: offset is relative to the current position

❑ SEEK_END: offset is relative to the end of the file

fstat, stat, and lstat

//函数定义:通过文件名获取文件的索引结点,放在结构里
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);

dup

//函数定义:复制文件描述符, 以实现多个进程共享同一个文件
#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);

fopen(库函数)

//函数定义:返回一个指向FILE类型结构体的指针。如果文件打开成功,则返回指向FILE类型结构体的指针;否则返回NULL。
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

❑ “r” or “rb”: Open for reading only

❑ “w” or “wb”: Open for writing, truncate to zero length

❑ “a” or “ab”: Open for writing, append to end of file

❑ “r+” or “rb+” or “r+b”: Open for update (reading and writing)

❑ “w+” or “wb+” or “w+b”: Open for update, truncate to zero length

❑ “a+” or “ab+” or “a+b”: Open for update, append to end of file

The b indicates that the file is a binary file rather than a text file.

fread(通过文件流指针读写)

#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
/*ptr:指向存储读取数据的缓冲区的指针。size:每个数据项的字节数。nitems:要读取的数据项的数量。stream:指向FILE类型结构体的指针,代表要读取的文件流。库函数的读写是记录式读写
*/

fwrite(通过文件流指针读写)

#include <stdio.h>
size_t fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream);

fclose

#include <stdio.h>
int fclose(FILE *stream);

fflush(刷新)

#include <stdio.h>
int fflush(FILE *stream);

fseek

//设置读写的指针
#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);
//offset:偏移位置
//whence:对位置的解释,是绝对位置还是相对位置还是从末尾数的位置...

opendir

//打开一个目录
#include <sys/types.h>	
#include <dirent.h>
DIR *opendir(const char *name);

readdir

//读取目录的记录
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
http://www.lryc.cn/news/69478.html

相关文章:

  • 【自制C++深度学习推理框架】Tensor模板类的设计思路
  • linux--systemd、systemctl
  • 加密解密软件VMProtect教程(七):主窗口之控制面板“详情”部分
  • 国产仪器 4945B/4945C 无线电通信综合测试仪
  • 数据库原理及应用上机实验一
  • 【操作系统】线程常用操作
  • C++编译预处理
  • Spring IOC 的理解
  • Linux 学习笔记(七):时间片
  • java并发-ReentrantLock
  • 21.模型的访问器和修改器
  • 72 yaffs文件系统挂载慢 sync不起作用
  • 【无标题】春漫乌海湖!
  • Red Hat重置root密码
  • 应急响应之日志排查方法,Linux篇
  • Midjourney AI 官方中文版已开启内测申请;OpenAI 正准备向公众发布一款新的开源语言模型。
  • DevOps 的道术法器,探寻 DevOps “立体化”实践之旅
  • redis 7.x 缓存双写一致性的解决方案
  • 真题详解(语法分析输入记号流)-软件设计(八十)
  • ffmpeg-编译汇总01
  • 素雅的登录界面,简单而优雅
  • Docker数据目录迁移方法
  • C++——动态规划
  • 【FAQ】视频编辑服务常见问题及解答
  • JavaEE(系列8) -- 多线程案例(单例模式)
  • 深度剖析,如何从底层代码层面理解Selenium和Appium的关联
  • 【Three.js】第一、二章 入门指南和基础知识
  • 力扣第 104 场双周赛 2681. 英雄的力量
  • 在linux上创建crypto_LUKS格式的块设备
  • 76.建立一个主体样式第二部分