Linux中所有的设备和文件的操作都使用文件描述符来进行。

文件描述符是一个非负的整数,它是一个索引值,指向内核中每个进程打开的记录表。

当打开一个文件或者创建一个新文件时,内核就向进程返回一个文件描述符;当需要读写文件的时候,就要把文件描述符作为参数传递给相应的函数。

一个进程时,都会打开3个文件:标准输入、标准输出和标准出错处理。分别对应文件描述符的0、1、2。

rename函数

#include <stdio.h>

int rename(const char *oldpath, const char *newpath);

说明:oldpath是文件的原路径, newpath是文件的新路径。

功能:文件重命名 。 返回值:运行成功返回0。

remove函数

#include <stdio.h>

int remove(const char *pathname);

说明:pathname是文件的路径。 功能:删除文件 。 返回值:运行成功返回0。

chown函数

#include <sys/types.h>

#include <unistd.h>

int chown(const char *path, uid_t owner, gid_t group);

说明:path是要修改文件的路径,owner指定的所有者, group是提定的文件组。 功能:修改文件的所有者 。 返回值:运行成功返回0,否则-1。

chmod函数。

#include <sys/types.h>

#include <sys/stat.h>

int (const char *path, mode_t mode); 说明:修改文件的访问权限

getcwd函数

#include <unistd.h>

char *getcwd(char *buf, size_t size);

说明:buf 是返回的目录名,buf 和size可以同时为设置0 返回值:成功是目录名 错误-1

#include<unistd.h>

#include<sdtio.h>

int main(int argc, char* argv[])

{

chdir("/tmp");

printf("current working directory: %s\n",getcwd(NULL,NULL));

}

执行 current working directory :/tmp

chdir函数 #include <unistd.h> i

nt chdir(const char *path);

说明:指定的目录路径。 功能:改变当前工作目录 。

返回值:运行成功返回0,否则-1。

opendir函数

#include <sys/types.h>

#include <dirent.h>

DIR *opendir(const char *name); 说明:指定的目录路径。

功能:打开目录 。

返回值:运行成功返回目录的目录流,否则NULL。

closedir函数

#include <sys/types.h>

#include <dirent.h>

int closedir(DIR *dir);

说明:已打开的目录。 功能:关闭已打开的目录 。

返回值:运行成功返回目录的目录流,否则NULL。

readdir函数

#include <sys/types.h>

#include <dirent.h>

struct dirent * readdir(DIR *dir);

说明:读取目录中一个文件的名字并将目录流指针后移。

功能:读取目录中一个文件的名字,并将目录流指针后移。

返回值:返回一个文件的名字,到目录尾或发生错误返回NULL。

struct dirent {

ino_t d_ino;

ff_t d_ff;

signed short int d_reclen;

unsigned char d_type;

char d_name[256];

};

telldir函数

#include <sys/types.h>

#include <dirent.h>

off_t telldir(DIR *dir); 说明:指定的目录流。 功能:获得指定目录流当前指针位置 。

返回值:运行成功返回当前指针位置,否则-1。

seekdir函数

#include <sys/types.h>

#include <dirent.h>

void seekdir(DIR *dir, off_t offset);

说明: dir是指定的目录流,offset是指定的指针位置 功能:设置指定的目录流的指针位置。

返回值:无。

fopen函数

#include <stdio.h>

FILE *fopen(const char *path, const char *mode);

说明: mode是文件打开的模式,r以只读形式打开,r+可读写形式打开,文件内容被清空,a,以追加方式打开,文件不存则新建,a+追加可读写形式打开,文件不存则新建。

功能:打开文件,获取文件流指针 。 返回值:文件的流指针。

#include <stdio.h>

main(){

FILE * fp =fopen(0,”w+”);

fprintf(fp,”%s\n”,”hello!”);

fclose(fp);}

fclose函数

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

说明: 文件流指针。 功能:关闭已打开的文件 。

返回值:运行成功返回当前指针位置,否则EOF。

ftell函数

#include <stdio.h> long ftell(FILE *stream);

说明: 文件流指针。 功能:获取文件流指针当前的读写位置 。

返回值:运行成功返回当前读写位置,否则-1。

fseek函数

#include <stdio.h>

int fseek(FILE *stream, long offset, int whence);

说明: 文件流指针。

功能: stream是指定的文件流指针, offset是指定的读写位置,

whence是设置读写位置的基准 。 返回值:运行成功返回0,否则-1。

#include <stdio.h>

main(){

FILE * stream;long offset;

fpos_t pos;

stream=fopen(“/etc/passwd”,”r”);

fseek(stream,5,SEEK_SET);

printf(“offset=%d\n”,ftell(stream));

rewind(stream);

fgetpos(stream,&pos);

printf(“offset=%d\n”,pos);

pos=10;

fsetpos(stream,&pos);

printf(“offset = %d\n”,ftell(stream));fclose(stream);

}执行 offset = 5offset =0offset=10

feof函数

#include <stdio.h>

int (FILE *stream);

说明: 文件流指针。

功能:判断文件流指针的当前读写位置是否已达到文件尾 。

返回值:达到文件尾返回非0,否则0。

fgetc函数

#include <stdio.h>

int fgetc(FILE *stream);

说明: 文件流指针。

功能:从指定的文件流中读取一个字符 。

返回值:读到的字符,达到文件尾时返回EOF。

#include <stdio.h>

main(){

FILE*fp;

int c;

fp=fopen(“exist”,”r”);

while((c=fgetc(fp))!=EOF)

printf(“%c”,c);

fclose(fp);

}

fputc函数

#include <stdio.h>

int fputc(int c, FILE *stream);

说明: c是要写入的字符,stream是指定的文件流指针。

功能:将单个字符写入指定的文件流中 。 返回值:运行成功返回参数c,失败返回EOF

fgets函数

#include <stdio.h>

char *fgets(char *s, int size, FILE *stream);

说明: s是字符串读取后存放的首地址, size是存放字符串的数据体积,( size -1)。

功能:从指定的文件流中读取一个字符串 。 返回值:运行成功返回s,失败返回NULL。

fputs函数

#include <stdio.h>

int fputs(char *s, FILE *stream);

说明: S是要写入的字符,stream是指定的文件流指针。

功能:将字符串写入指定的文件流中 。

返回值:运行成功返回写入文件中字符的个数,失败返回EOF。

fread函数

#include <stdio.h>

size_t fread(void *ptr, size_t size, size_t nmenb, FILE *stream);

说明: ptr是读取的数据在内存中存放地址, nmenb 是要读取的数据单元个数。

功能:从指定文件流中读取一段数据 。 返回值:实际读取到的数据单元个数。

fwrite函数

#include <stdio.h>

size_t  fwrite(void *ptr, size_t size, size_t nmenb, FILE *stream);

说明: ptr是读取的数据在内存中存放地址, nmenb 是要读取的数据单元个数。

功能:从将指定的一段数据写入指定文件流。 返回值:实际读取到的数据单元个数。

open函数

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int open(const char *pathname, int flags, mode_t mode);

O_RDONLY 以只读的形式打开文件,

O_WRONLY 以只写的形式打开文件,

O_RDWR 以可读写的形式打开文件;

O_CREAT 如果被打开文件不存在,则自动建立文件,

O_EXCL 与O_CREAT一起使用,若文件存在则报错;

O_TRUNC 以可写方式打开时,清空文件内容。

说明:pathname是要打开文件的路径,flags是标志位,mode用来指明文件的操作权限。

功能:打开指定文件,返回文件标识符。 返回值:成功 文件标识符 错误 -1

creat函数

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int creat(const char *pathname, mode_t mode);

说明: 相当于调用open()。

功能:创建新文件 。

返回值:运行成功返回文件标识符,失败返回0。

mktemp函数

#include <stdlib.h>

int mktemp(char *template);

说明:临时文件的文件名,最后六个字符必须为XXXXXX 。

功能:创建临时内存文件 。

返回值:运行成功返回文件标识符,失败返回0。

close关闭文件

#include <unistd.h>

int close(int fd);

read函数

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t count);

说明:fd是文件标识符,buf是内存中的数据缓冲区,count是要读取数据的大小 。

功能:从指定的文件中读取数据 。

返回值:运行成功返回读取数据的长度(多少个字节),失败返回-1。

write函数

#include <unistd.h>

ssize_t write(int fd, void *buf, size_t count);

说明:fd是文件标识符,buf是内存中的数据缓冲区,count是要读取数据的大小 。

功能:将数据写入指定的文件中 。

返回值:运行成功返回读取数据的数据,失败返回-1。

lseek函数

#include <sys/types.h>

#include <unistd.h>

off_t lseek(int fd, off_t offset, int whence);

说明:fd是文件标识符,offset是移动的偏移量。

Whence是移动的基准位置 。

功能:修改文件的读写位置。

返回值:运行成功返回读写指针后文件实际读写位置的绝对量,失败返回-1。

fcntl函数

#include <sys/types.h>

#include <unistd.h>

#include <fcntl.h>

int fcntl(int fd, int cmd, struct flock *lock);

说明:fd是文件标识符,cmd 是命令,lock记录锁的具体状态。

功能:处理文件共享问题。 返回值:0 成功 -1 出错

select函数

#include <sys/types.h>

#include <unistd.h>

#include <sys/time.h>

int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)

说明:numfds需要检查的号码最高的文件描述符加1,

readfds由select监视的读文件描述符集合,

writefds由select监视的写文件描述符集合,

errorfds由select监视的异常处理文件描述符集合,

timeout:NULL 永远等待,知道扑捉到信号或者描述符已准备好为止,具体指若等待位timeout时间还没有文件描述符准备好,就立即返回,0 从不等待,测试所有指定的描述符并立即返回。 功能:处理I/O复用问题。 返回值: 成功 准备好的文件描述符 -1 出错

pipe函数 #include <unistd.h>

int pipe(int filedes[2]);

说明:管道两端对应的文件标识符,fi

ldes[0]为读端的标识符, fildes[1]为写端的标识符。

功能:创建无名管道 。 返回值:运行成功返回0,失败返回-1。

mkfifo函数

#include <sys/types.h>

#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);

说明:pathname是管道的文件名,mode是管道的权限。

功能:创建有名管道,该管道被看作一个文件创建。

返回值:运行成功返回0,失败返回-1。

linux 文件操作编程的更多相关文章

  1. 【API】文件操作编程基础-CreateFile、WriteFile、SetFilePointer

    1.说明 很多黑客工具的实现是通过对文件进行读写操作的,而文件读写操作实质也是对API函数的调用. 2.相关函数 CreateFile : 创建或打开文件或I/O设备.最常用的I/O设备如下:文件,文 ...

  2. C/C++以及Linux文件操作备忘录

    目录 C文件操作 文件开关 文件读写 C++文件操作 Linux文件操作 打开 C文件操作 #include<stdio.h> stdin, stdout, stderr 文件开关 /* ...

  3. Linux 文件操作接口

    目录 Linux 文件操作接口 C语言文件操作接口 C语言文件描述 fopen() r模式打开文件 w模式打开文件 a模式打开文件 其他模式类似 fclose() fwrite() fread() 系 ...

  4. Linux 文件操作——系统调用和标准I/O库

    一.什么是文件 在讲述文件操作之前,我们首先要知道什么是文件.看到这个问题你可能会感觉到可笑,因为对于用过计算机的人来说,文件是最简单不过的概念了,例如一个文本是一个文件,一个work文档是一个文件等 ...

  5. Linux文件操作的主要接口API及相关细节

    操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...

  6. Linux 文件操作监控inotify功能及实现原理【转】

    转自:http://blog.chinaunix.net/uid-26585427-id-5012973.html 1. inotify主要功能 它是一个内核用于通知用户空间程序文件系统变化的机制. ...

  7. Linux 文件操作总结

    http://blog.163.com/he_junwei/blog/static/19793764620152592737741/ ioctl?? lseek?? 文件是linux中的一个重要概念. ...

  8. Linux文件操作学习总结【转载】

    本文转载自: http://blog.csdn.net/xiaoweibeibei/article/details/6556951 文件类型:普通文件(文本文件,二进制文件).目录文件.链接文件.设备 ...

  9. 2.Linux文件IO编程

    2.1Linux文件IO概述 2.1.0POSIX规范 POSIX:(Portable Operating System Interface)可移植操作系统接口规范. 由IEEE制定,是为了提高UNI ...

随机推荐

  1. PC-信使服务之不用聊天软件也能通信

    net send 192.168.1.2 OK 二台电脑都要开启messenger服务.

  2. 第一章 Windows NT System Components

    Page 3. The focus(焦点) of this book is Windows NT file system and the interaction(交互) of the file sys ...

  3. redis整合spring @Bean写法

    jedis是一款java连接redis的客户端,spring基于jedis进行了封装,提供了简洁的操作redis的方法. 使用maven进行管理jar包之间的依赖: <dependency> ...

  4. 【开发工具 - Git】之Git版本回退

    这篇博客主要记录了关于 查看记录.版本回退.添加标签.删除文件 的操作 1.查看文件修改情况: 可以通过 git diff a.java查看a.java文件自从上次提交后的修改情况,如果自从上次提交之 ...

  5. c# 判断网络是连接到互联网

    方法1:InternetGetConnectedState         [System.Runtime.InteropServices.DllImport("wininet") ...

  6. jstl前台Jsp日期格式化

    1. 引入fmt标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %&g ...

  7. Error: no `server' JVM at...解决办法 【转】

    出现问题:用java -jar XXX.jar -server -Xms900m -Xmx900m 提示错误Error: no `server' JVM at `C:\Program Files\Ja ...

  8. 如何编译libcurl

    1. Android •1.1配置 •1.2 Make •1.3的参数配置 2.iOS 3.windows 4.关于头文件 注释 本文档介绍了如何为Android,iOS和Windows编译libcu ...

  9. [转] Android:微信授权登录与微信分享全解析

    https://wohugb.gitbooks.io/wechat/content/qrconnent/refresh_token.html http://blog.csdn.net/xiong_it ...

  10. poj 1007 DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95437   Accepted: 38399 Des ...