文件读写

简介: 文件读写

write()函数

标准I/O相比,文件I/O对文件的操作更简单直接。 write() 函数用于将数据写入一个已经打开的文件,并返回实际写入的字节数。函数并没有固定写入文件的数据格式,只需要按照字节数操作即可。


注意

没有缓存区的操作。


#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);点击复制复制失败已复制


参数 fd 为已经打开的文件描述符,参数 buf 保存的是写入文件的数据,参数 count 为从 buf 中需要写入文件的字节数。


使用示例:

source.h 文件中定义结构体:

#ifndef _SOURCE_H
#define _SOURCE_H
#define N 32
struct  data
{
  int a;
  char b;
  char buf[N];
};
#endif点击复制复制失败已复制


接下来在 main.c 文件中写入逻辑代码:

#include "source.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#define errlog(errmsg)                                                         \
  perror(errmsg);                                                              \
  printf("--%s--%s--%d--\n", __FILE__, __FUNCTION__, __LINE__);                \
  return -1;
struct data obj = {.a = 1, .b = 'w', .buf = "test"};
int main(int argc, const char *argv[]) {
  int fd;
  if ((fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0) {
    errlog("open error");
  }
  if (write(fd, &obj, sizeof(struct data)) < 0) {
    errlog("write error");
  }
  close(fd);
  return 0;
}点击复制复制失败已复制


编译运行:

$ gcc main.c && ./a.out
$ od -c test.txt 
0000000 001  \0  \0  \0   w   t   e   s   t  \0  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000040  \0  \0  \0  \0  \0  \0  \0  \0
0000050点击复制复制失败已复制


read() 函数

read() 函数从文件描述符 fd 指代的打开文件中读取数据,并返回实际读取的字节数。若返回 0 ,则表示当前读写位置位于文件末尾。与标准I/O的操作同理,每一次的读写都会产生读写位置的偏移。


语法如下:

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);点击复制复制失败已复制


read() 函数参数与 write() 函数一致,只是数据的方向发生了变化。参数 buf 用于保存读取的数据,参数 count为 期望读取的字节数,实际读取的字节数只可能小于或等于count


使用示例:

#include "source.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#define errlog(errmsg)                                                         \
  perror(errmsg);                                                              \
  printf("--%s--%s--%d--\n", __FILE__, __FUNCTION__, __LINE__);                \
  return -1;
int main(int argc, const char *argv[]) {
  int fd;
  struct data obj;
  ssize_t nbyte;
  if ((fd = open("test.txt", O_RDONLY)) < 0) {
    errlog("open error");
  }
  if ((nbyte = read(fd, &obj, sizeof(struct data))) > 0) {
    printf("nbyte = %ld\n", nbyte);
    printf("a = %d b = %c buf = %s\n", obj.a, obj.b, obj.buf);
  }
  close(fd);
  return 0;
}点击复制复制失败已复制


提示

复用上面的 write() 示例中的 source.h 和生成的 test.txt 文件。


编译运行:

$ gcc main.c && ./a.out 
nbyte = 40
a = 1 b = w buf = test
目录
相关文章
|
1月前
|
存储 程序员 C语言
C文件读写
【2月更文挑战第14天】C文件读写。
13 1
|
6月前
|
存储 C语言
C 文件读写
C 文件读写。
24 0
|
3月前
|
存储 移动开发 Linux
C++017-C++文件读写应用
C++017-C++文件读写应用
|
4月前
|
存储 Java C语言
文件操作你会了吗
文件操作你会了吗
41 0
|
5月前
|
存储 C++ iOS开发
70 C++ - 文件读写
70 C++ - 文件读写
32 0
|
7月前
|
C#
使用C#进行文件操作
在许多应用程序中,文件操作是常见的任务之一。无论是读取文件内容、写入数据,还是创建、移动和删除文件,C# 编程语言都提供了强大且易于使用的文件操作功能。本篇博客将介绍如何使用C#来进行基本的文件操作。
35 0
|
存储 编译器 程序员
文件操作(详解)
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件, 或者输出内容的文件。
92 0
文件操作(详解)
|
开发者 Python
文件读写
文件读写
44 0
|
存储 iOS开发 C++
c++文件读写操作
c++文件读写操作
|
数据库 Windows
深入理解文件操作——C(1)
深入理解文件操作——C(1)
深入理解文件操作——C(1)