文件操作
系統(tǒng)調(diào)用
write
//函數(shù)定義
#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
//函數(shù)定義
#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);//打開(kāi)文件后,0代表標(biāo)準(zhǔn)輸入,1代表標(biāo)準(zhǔn)輸出,2代表錯(cuò)誤輸出
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 會(huì)將字符串 "hello there" 輸出到終端上。
# | 是管道符,它將前面輸出的內(nèi)容作為后面命令的輸入。
# simple_read 是一個(gè)命令行程序,它會(huì)從標(biāo)準(zhǔn)輸入中讀取一行文本,并將其打印到標(biāo)準(zhǔn)輸出上。
# 將字符串 "hello there" 通過(guò)管道傳遞給 simple_read,simple_read 會(huì)將其讀取并打印到終端上。
hello there
$ simple_read < draft1.txt #將文件" draft1.txt "的內(nèi)容顯示在終端
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
//函數(shù)定義
#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
//函數(shù)定義
#include <unistd.h>
int close(int fildes);
//應(yīng)用示例:文件復(fù)制的程序
#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
//函數(shù)定義:確定文件的讀寫(xiě)位置
#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
//函數(shù)定義:通過(guò)文件名獲取文件的索引結(jié)點(diǎn),放在結(jié)構(gòu)里
#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
//函數(shù)定義:復(fù)制文件描述符, 以實(shí)現(xiàn)多個(gè)進(jìn)程共享同一個(gè)文件
#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);
fopen(庫(kù)函數(shù))
//函數(shù)定義:返回一個(gè)指向FILE類型結(jié)構(gòu)體的指針。如果文件打開(kāi)成功,則返回指向FILE類型結(jié)構(gòu)體的指針;否則返回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(通過(guò)文件流指針讀寫(xiě))
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
/*
ptr:指向存儲(chǔ)讀取數(shù)據(jù)的緩沖區(qū)的指針。
size:每個(gè)數(shù)據(jù)項(xiàng)的字節(jié)數(shù)。
nitems:要讀取的數(shù)據(jù)項(xiàng)的數(shù)量。
stream:指向FILE類型結(jié)構(gòu)體的指針,代表要讀取的文件流。
庫(kù)函數(shù)的讀寫(xiě)是記錄式讀寫(xiě)
*/
fwrite(通過(guò)文件流指針讀寫(xiě))
#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
//設(shè)置讀寫(xiě)的指針
#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);
//offset:偏移位置
//whence:對(duì)位置的解釋,是絕對(duì)位置還是相對(duì)位置還是從末尾數(shù)的位置...
opendir文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-454904.html
//打開(kāi)一個(gè)目錄
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
readdir文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-454904.html
//讀取目錄的記錄
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
到了這里,關(guān)于Linux程序設(shè)計(jì):文件操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!