库函数与系统调用(常用IO操作)

fopen,fclose,fwrite,fread,fseek,open,close,read,write等

一、库函数与系统调用

1.系统调用
操作系统会对外表现为一个整体,但是会暴露自己的部分接口,这由操作系统提供的部分接口,称为系统调用。
2.库函数
系统调用进行适度封装,从而形成了库,有利于开发者进行二次开发。

二、C库IO操作

1.fread:从文件流中读数据

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
ptr
用于接收数据的内存地址
size
每个数据项的字节数,单位是字节
nmemb
读nmemb个数据项,每个数据项size个字节.
stream
输入流

2.fwrite:往文件流中写数据

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
RETURN VALUE:返回实际写入的数据项个数nmemb。
(1)ptr:是一个指针,对fwrite来说,是要获取数据的地址;
(2size:要写入内容的单字节数;
(3)nmenmb:要进行写入size字节的数据项的个数;
(4)stream:目标文件指针;

注:完成一次写/读操作后必须关闭流,如果没有关闭流(fclose()),则指针(FILE * ptr)自动向后移动前一次读写的长度,不关闭流,那么继续下一次读操作则接着上次的输出继续输出;
3.fseek:改变文件指针指向

int fseek(FILE *stream, long offset, int whence);
第一个参数stream为文件指针
第二个参数offset为偏移量,正数表示正向偏移,负数表示负向偏移
第三个参数whence,设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
SEEK_SET: 文件开头
SEEK_CUR: 当前位置
SEEK_END: 文件结尾
其中SEEK_SET,SEEK_CUR和SEEK_END依次为012.

4.fopen:打开一个文件

FILE *fopen(const char *path, const char *mode);
r      Open text file for reading.  The stream is positioned at the beginning of the file.
r+     Open for reading and writing.  The stream is positioned at the beginning of the file.
w      Truncate file to zero length or create text file for writing.  The stream is positioned at the beginning of the file.
w+     Open  for  reading  and writing.  The file is created if it does not exist, otherwise it is truncated.  The stream is positioned at the beginning of the file.
a      Open for appending (writing at end of file).  The file is created if it does  not  exist.   The  stream  is positioned at the end of the file.
a+     Open  for  reading  and appending (writing at end of file).  The file is created if it does not exist.  The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

5.fclose:关闭一个文件,并会刷新执行流。

int fclose(FILE *fp);

这里写图片描述

文件操作示例:

#include <stdio.h>
#include <string.h>
int main()
{
    FILE* stream=fopen("abc.txt","w+");
    if(stream==NULL)
    {
        perror("fopen");
        return -1;
    }
    char* msg="hello";
    char buf[20];
    fwrite(msg,sizeof(char),strlen(msg)+1,stream);//把字符串内容写到文件中
    fseek(stream,0,SEEK_SET);//定位文件指针到文件开始位置
    fread(buf,sizeof(char),strlen(msg)+1,stream);//将文件里的内容写入到缓存里
    printf("%s\n",buf);
    fclose(stream);
    stream=NULL;//防止出现游离指针
    return 0;
}

6:如何输出信息到显示器?

const char *msg="hello\n";
printf("%s",msg);                                                                                                              
fprintf(stdout,msg);
fwrite(msg,sizeof(char),strlen(msg)+1,stdout);

7:stdin&stdout&stderr

extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;

三、系统调用IO操作

1.open

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
返回值:成功则返回文件描述符,否则返回-1

参数一些说明:
1)对于open函数来说,第三个参数仅当创建新文件时(即使用了O_CREAT 时)才使用,用于指定文件的访问权限位。
2)flags

O_RDONLY只读模式
O_WRONLY只写模式
O_RDWR读写模式

打开/创建文件时,以上三个常量必须指定一个且只能指定一个。以下常量是选用的:

O_APPEND每次写操作都写入文件的末尾
O_CREAT如果指定文件不存在,则创建这个文件
O_EXCL如果要创建的文件已存在,则返回-1,并且修改errno的值
O_TRUNC如果文件存在,并且以只写/读写方式打开,则清空文件全部内容(即将其长度截短为0)

2.close

#include <unistd.h>
int close(int fd);
返回值:
close() returns zero on success.  On error, -1 is returned, and errno is set appropriately.

3.read:从文件里读到缓冲区

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
//count为读到的字节数
//返回读取的字节数

注意:
read时fd中的数据如果小于要读取的数据,就会引起阻塞。可以添加换行符(回车),就不会阻塞了。
从终端设备读,通常以行为单位,读到换行符就返回了换行符也算读到的内容

int main()
{
    char buf[1024];
    ssize_t s=read(0,buf,sizeof(buf)-1);//从屏幕中读取数据,包括换行符,
    printf("%d\n",sizeof(buf)-1);
    printf("s=%d\n",s);
    buf[s]=0;
    printf("%d\n",strlen(buf));
    printf("%d\n",sizeof(buf));                                                                                                                 
    return 0;
}

这里写图片描述
从结果可以看出,比如abc\n,它的strlen为4。

4.write:将缓冲区内容往文件里写

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

参数count必须是实际写入的字节数。

猜你喜欢

转载自blog.csdn.net/guorong520/article/details/79836776