系统编程之文件编程
Linux文件可分为:普通文件,目录文件,链接文件,设备文件;
1、文件打开与创建
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
参数1:pathname,要打开或创建的文件名字
参数2:flags,文件选择打开的方式 O_RDONLY 只读打开 O_WRONLY 只写打开 O_WRONLY 只写打开
参数3:mode,文件权限
2、close()打开文件之后要关闭
int read(int fd, void *buf, size_t count);
fd: 文件描述符 buf:存储将要读入的数据
count: 读出的长度,以字节为单位 返回值:读成功时,返回读出的字符长度,否则返回-1。
int write(int fd, const void *buf, size_t count);
fd: 文件描述符
buf:存储将要写的数据
count: 写入的长度,以字节为单位
返回值:写入成功时,返回写入的字符长度,否则返回-1。
int lseek(int fd, off_t offset, int whence);
fd: 文件描述符 offset:将要偏移 的字节数。取负值表示向前移动。
whence:从哪开始偏移宏定义如下:
SEEK_END 相对文件末尾 SEEK_CUR 相对文件读写指针当前偏移量位置
SEEK_SET 相对文件开头位置
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
int fd, ret; //file describtion 文件描述符
char buf[15] = "hellohello";
#if 0
fd = open("hello.c", O_RDWR); //以读写方式打开文件hello.c
if(-1 == fd)
{
perror("open");
exit(1);
}
close(fd);
#endif
fd = open("hello.txt", O_RDWR | O_CREAT, S_IRWXU); //创建文件hello.txt并以读写方式打开
if(-1 == fd)
{
perror("open");
exit(1);
}
ret = write(fd, buf, strlen(buf));
if(-1 == ret)
{
perror("write");
exit(1);
}
ret = lseek(fd, 0, SEEK_SET);//相对文件首位置向后移动长度0
if(-1 == ret)
{
perror("lseek");
exit(1);
}
memset(buf, 0, sizeof(buf));
ret = read(fd, buf, sizeof(buf));
if(ret == -1)
{
perror("read");
exit(1);
}
printf("read from txt : %s\n", buf);
close(fd);
return 0;
}
FILE *fopen(const char *filename, const char *mode)
filename:打开的文件名(包含路径,缺省为当前路径)
mode: 打开模式
r, rb 只读方式打开,文件必须已存在 w, wb 只写方式打开,如果文件不存在则创,如果文件已存在清空重写
a, ab 只能在文件末尾追加数据,如果文件不存在则创建 r+,rb+, r+b 读写方式打开,文件必须已存在
w+,w+b, wb+ 读写方式打开,如果文件不存在则创建,如果文件已存在清空重写
a+, a+b, ab+ 读和追加方式打开,如果文件不存在则创建
int fclose(FILE * stream);
fclose()用来关闭先前fopen()打开的文件。此动作会让缓冲区内的数据写入文件中,并释放系统所提供的文件资源
返回值:若关文件动作成功则返回0,有错误发生时则返回EOF并把错误代码存到errno。
int fseek(FILE *stream, long offset, int whence);
stream:要重新定位的文件指针; offset: 相对于第三个参数的偏移量
whence:文件指针相对于文件的位置。
int fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) ;
ptr:存放想要写入文件的数据; size:每块数据的大小;
nmemb:读取的数据块数; steam:希望写入的文件的文件指针;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
int ret;
char buf[32] = {0};
fp = fopen("hello.c", "a+"); //读和追加的方式打开hello.c,不存在则创建
if(NULL == fp)
{
perror("fopen");
exit(1);
}
ret = fseek(fp, 0, SEEK_SET);
if(-1 == ret)
{
perror("fseek");
exit(1);
}
ret = fread(buf, 1, sizeof(buf), fp);
if(-1 == ret)
{
perror("fread");
exit(1);
}
printf("read from txt : %s\n", buf);
memset(buf, 0, sizeof(buf));
scanf("%s", buf);
ret = fwrite(buf, 1, strlen(buf), fp);
if(ret < 0)
{
perror("fwrite");
exit(1);
}
fclose(fp);
return 0;
}
文件长度
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
if(argc != 2)
{
printf("Input Error!\n");
exit(1);
}
fp = fopen(argv[1], "r");
if(NULL == fp)
{
perror("fopen");
exit(1);
}
fseek(fp, 0, SEEK_END); //文件指针移动到文件末尾
int length = ftell(fp);
printf("length is %d\n", length);
fclose(fp);
return 0;
}