c语言IO

前言

老是忘记c语言IO操作,故写个文章记录一下

打开文件

fopen

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

modeimage-20240526205319442

返回值

  • 如果文件成功打开,fopen 返回一个指向 FILE 结构的指针。
  • 如果文件打开失败(例如,因为文件不存在、没有足够的权限、磁盘空间不足等),fopen 返回 NULL

使用

#include<stdio.h>
int main()
{
    FILE* new_file = fopen("file.txt", "w");
}

open

#include <fcntl.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
int open(const char *pathname, int flags, ...); //flag可以用“ | ”组合

常用的flags

  • O_RDONLY:只读方式打开文件。
  • O_WRONLY:只写方式打开文件。
  • O_RDWR:读写方式打开文件。
  • O_APPEND每次写操作都写入文件的末尾
  • O_CREAT如果指定文件不存在,则创建这个文件,指明权限
  • O_EXCL如果要创建的文件已存在,则返回-1,并且修改errno的值
  • O_TRUNC如果文件存在,并且以只写/读写方式打开,则清空文件全部内容(即将其长度截短为0)
  • O_NOCTTY如果路径名指向终端设备,不要把这个设备用作控制终端。 6
  • O_NONBLOCK如果路径名指向FIFO/块文件/字符文件,则把文件的打开和后继I/O

权限

S_IRWXU, 700权限,代表该文件所有者具有可读、可写及可执行的权限。 S_IRUSR或S_IREAD, 400权限,代表该文件所有者具有可读取的权限。 S_IWUSR或S_IWRITE, 200权限,代表该文件所有者具有可写入的权限。 S_IXUSR或S_IEXEC, 100权限,代表该文件所有者具有可执行的权限。

S_IRWXG, 070权限,代表该文件用户组具有可读、可写及可执行的权限。 S_IRGRP, 040权限,代表该文件用户组具有可读的权限。

S_IWGRP, 020权限,代表该文件用户组具有可写入的权限。

S_IXGRP, 010权限,代表该文件用户组具有可执行的权限。

S_IRWX, O07权限,代表其他用户具有可读、可写及可执行的权限。 S_IROTH, 004权限,代表其他用户具有可读的权限

S_IWOTH, 002权限,代表其他用户具有可写入的权限。

S_IXOTH, 001权限,代表其他用户具有可执行的权限。

返回值

如果成功,open 函数返回一个非负的文件描述符(file descriptor),它是一个小的非负整数,用于后续的文件操作(如 read、write、close 等)。如果失败,open 函数返回 -1,并设置全局变量 errno 以指示错误。

#include <fcntl.h>  
#include <unistd.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
  
#define BUFFER_SIZE 1024  
  
int main() 
{  
    int fd; // 文件描述符  
    char buffer[BUFFER_SIZE]; // 缓冲区  
    ssize_t bytesRead; // 读取的字节数  
  
    // 打开文件,以只读方式  
    fd = open("example.txt", O_RDONLY);  
 
    // 读取文件内容到缓冲区  
    bytesRead = read(fd, buffer, BUFFER_SIZE - 1); // 减去1以保留空间给字符串终止符  
}

读取文件

fgets

fgets 函数用于从指定的流中读取一行,并保存到字符串中。它会在遇到换行符(\n)、EOF(文件结束)或达到指定的大小限制时停止。

#include <stdio.h>  
  
int main() {  
    FILE *file = fopen("example.txt", "r");  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
  
    char line[100];  
    while (fgets(line, sizeof(line), file)) {  
        printf("%s", line);  
    }  
  
    fclose(file);  
    return 0;  
}

fcanf

fscanf 函数用于从指定的流中读取格式化的输入。

#include <stdio.h>  
  
int main() {  
    FILE *file = fopen("numbers.txt", "r");  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
  
    int num1, num2;  
    while (fscanf(file, "%d %d", &num1, &num2) == 2) {  
        printf("Read: %d and %d\n", num1, num2);  
    }  
  
    fclose(file);  
    return 0;  
}

fgetc

fgetc 函数用于从指定的流中读取一个字符

#include <stdio.h>  
  
int main() {  
    FILE *file = fopen("example.txt", "r");  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
  
    int ch;  
    while ((ch = fgetc(file)) != EOF) {  
        putchar(ch);  
    }  
  
    fclose(file);  
    return 0;  
}

fread

fread 函数用于从指定的流中读取一块数据。它通常用于二进制文件的读取。

#include <stdio.h>  
  
typedef struct {  
    int id;  
    char name[50];  
} Person;  
  
int main() {  
    FILE *file = fopen("people.bin", "rb");  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
  
    Person person;  
    while (fread(&person, sizeof(Person), 1, file) == 1) {  
        printf("ID: %d, Name: %s\n", person.id, person.name);  
    }  
  
    fclose(file);  
    return 0;  
}

read

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

/*
fd: 文件描述符(file descriptor),通常是一个非负整数,用于标识打开的文件或套接字。
buf: 指向一个缓冲区的指针,该缓冲区用于存储从文件中读取的数据。
count: 要读取的最大字节数。
   
返回值:
成功时,返回读取的字节数(可能小于 count)。
失败时,返回 -1 并设置 errno。*/

image-20240526211638573

#include <stdio.h>  
#include <unistd.h>  
#include <fcntl.h>  
#include <string.h>  
  
int main() {  
    int fd = open("file.txt", O_RDONLY);  
    if (fd == -1) {  
        perror("open");  
        return 1;  
    }  
  
    char buf[1024];  
    ssize_t n = read(fd, buf, sizeof(buf) - 1);  
    if (n == -1) {  
        perror("read");  
        close(fd);  
        return 1;  
    }  
  
    buf[n] = '\0'; // 添加字符串终止符  
    printf("Read %zd bytes: %s\n", n, buf);  
  
    close(fd);  
    return 0;  
}

写入文件

fputs

fputs函数用于将一个字符串写入到文件中。

int fputs(const char *str, FILE *stream);

#include <stdio.h>  
  
int main() {  
    FILE *file = fopen("output.txt", "w");  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
      
    const char *str = "Hello, World!";  
    fputs(str, file);  
      
    fclose(file);  
    return 0;  
}

fprintf

fprintf函数用于将格式化的数据写入到文件中。它的使用方式与printf函数非常相似,只是多了一个文件指针参数。

int fprintf(FILE *stream, const char *format, ...);

#include <stdio.h>  
  
int main() {  
    FILE *file = fopen("output.txt", "w");  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
      
    int number = 42;  
    fprintf(file, "The answer is: %d\n", number);  
      
    fclose(file);  
    return 0;  
}

fputc

fputc函数用于将一个字符写入到文件中。

int fputc(int char, FILE *stream);

#include <stdio.h>  
  
int main() {  
    FILE *file = fopen("output.txt", "w");  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
      
    char ch = 'A';  
    fputc(ch, file);  
    fputc('\n', file); // 写入换行符  
      
    fclose(file);  
    return 0;  
}

fwrite

fwrite函数用于将一块数据(可以是字符串、数组等)写入到文件中。它接收四个参数:一个指向要写入数据的指针、数据块的大小、数据块的数量以及文件指针。

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

#include <stdio.h>  
  
int main() {  
    FILE *file = fopen("output.txt", "wb"); // 使用"wb"以二进制模式写入  
    if (file == NULL) {  
        perror("Error opening file");  
        return 1;  
    }  
      
    char str[] = "Hello, World!";  
    size_t len = sizeof(str) - 1; // 不包括字符串终止符'\0'  
    fwrite(str, sizeof(char), len, file);  
      
    fclose(file);  
    return 0;  
}

write

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

/*
fd: 文件描述符。
buf: 指向一个包含要写入数据的缓冲区的指针。
count: 要写入的字节数。

返回值:
成功时,返回写入的字节数(可能小于 count,尤其是在非阻塞模式下)。
失败时,返回 -1 并设置 errno。*/

image-20240526211828390

#include <stdio.h>  
#include <unistd.h>  
#include <fcntl.h>  
#include <string.h>  
  
int main() {  
    int fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);  
    if (fd == -1) {  
        perror("open");  
        return 1;  
    }  
  
    const char *msg = "Hello, world!\n";  
    ssize_t n = write(fd, msg, strlen(msg));  
    if (n == -1) {  
        perror("write");  
        close(fd);  
        return 1;  
    }  
  
    printf("Wrote %zd bytes\n", n);  
  
    close(fd);  
    return 0;  
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值