Linux程序设计:文件操作

这篇具有很好参考价值的文章主要介绍了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文章来源地址https://www.toymoban.com/news/detail-454904.html

//读取目录的记录
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dirp);

到了这里,关于Linux程序设计:文件操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包