文件存储数据

需求

设计一个小程序实现功能,将这个结构体的数据,保存在文件中
需求分析

如果是想将数据以文本格式保存在txt文件中,则使用fprintf()函数

如果是想将数据以二进制保存在bin文件中,则使用fwrite()函数

写入文本文件源码

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define length (1024)
typedef struct
{
    char id[length];
    char pwd[length];
    int age;
    int number;
} info;

void saveConfigToFile(const char *filename, const info *config)
{

    FILE *file = fopen(filename, "w");
    if (file == NULL)
    {
        perror("Error opening file");
        return;
    }
    if (fprintf(file, "id: %s\n", config->id) < 0 ||
        fprintf(file, "PWD: %s\n", config->pwd) < 0 ||
        fprintf(file, "age: %d\n", config->age) < 0 ||
        fprintf(file, "number: %d\n", config->number) < 0)
    {
        perror("Error writing to file");
    }

    fclose(file);
}

int main()
{
    info config = {
        .id = "ags",
        .pwd = "asg",
        .age = 0,
        .number = 0};

    saveConfigToFile("config.txt", &config);
    
    return 0;
}

写入二进制文件源码

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define length (1024)
typedef struct
{
    char id[length];
    char pwd[length];
    int age;
    int number;
} info;

void saveConfigToFile(const char *filename, const info *config)
{

    FILE *file = fopen(filename, "wb");
    if (file == NULL)
    {
        perror("Error opening file");
        return;
    }
    if (fwrite(config, sizeof(info), 1, file) != 1)
    {
        perror("Error writing to file");
    }

    fclose(file);
}

int main()
{
    info config = {
        .id = "ags",
        .pwd = "asg",
        .age = 0,
        .number = 0};

    saveConfigToFile("config.bin", &config);
    return 0;
}

打开文件

函数原型

c 复制代码
 FILE *fopen(const char *pathname, const char *mode);

函数参数

  • 1.char *pathname

    一个字符串,表示要打开的文件的路径。这个路径可以是相对路径或绝对路径。

  • 2const char *mode

    一个字符串,指定文件打开的模式。这个模式决定了文件是用于读取、写入、追加还是其他操作。常见的模式有:

c 复制代码
"r":以只读方式打开文件。文件必须存在。
"w":以只写方式打开文件。如果文件存在,则将其长度截断为 0(即删除文件内容),如果文件不存在则创建它。
"a":以追加方式打开文件,写操作会从文件末尾开始。如果文件不存在,则创建它。
"r+":以读/写方式打开文件。该文件必须存在。
"w+":创建一个空文件用于读写。
"a+":以读/写模式打开文件,写操作会从文件末尾开始。如果文件不存在,则创建它。
"b":在文件路径字符串中可以包含一个 "b" 字符,它将文件打开为二进制模式,这在读写二进制文件时是有用的。

函数返回值

fopen 函数返回一个 FILE 指针,该指针指向新打开的文件。

如果打开文件失败,函数将返回 NULL。

在使用完文件后,应该使用 fclose 函数关闭文件。

字符串比较

  • 1.函数原型
c 复制代码
int strcmp(const char *s1, const char *s2);
  • 2.函数参数

    *const char s1

    待比较的字符串1

    *const char s2

    待比较的字符串2

  • 3.函数返回值

    如果 s1 等于 s2,strcmp 返回 0。

    如果 s1 小于 s2(在字典顺序上),strcmp 返回一个负整数。

    如果 s1 大于 s2(在字典顺序上),strcmp 返回一个正整数。

写入数据fprintf

fprintf 函数是 C 语言标准库中的一个函数,用于将格式化的数据写入文件。它定义在 <stdio.h> 头文件中,其原型如下:

  • 1.函数原型
c 复制代码
int fprintf(FILE *stream, const char *format, ...);
  • 2.函数参数
    stream
    一个 FILE 类型的指针,指向 FILE 对象,这个对象标识了要写入数据的文件流。它可以是标准输出 stdout、标准错误 stderr 或者通过 fopen 打开的文件指针。

format

一个格式字符串,它指定了后续参数如何被格式化。格式字符串中可以包含格式化指定符,如 %d 用于整数,%s 用于字符串,%f 用于浮点数等。

  • 3.函数返回值
    fprintf 函数的返回值是成功写入的字符数。
    如果发生错误,它将返回一个负数。

写入数据fwrite

fwrite 函数是 C 语言标准库中的一个函数,用于将数据写入文件。它定义在 <stdio.h> 头文件中,其原型如下:

  • 1.函数原型
c 复制代码
size_t fwrite(const void *ptr, size_t size, size_t nmemb,
                     FILE *stream);
c 复制代码
fwrite(config, sizeof(stWFMUsrApCfgInfo), 1, file)

-2.函数参数

ptr

一个指向数据块的指针,这个数据块中的内容将被写入文件。

此处是一个结构体
size

每个数据项的大小,以字节为单位。
nmemb

要写入的数据项的数量。
stream

一个 FILE 类型的指针,指向 FILE 对象,这个对象标识了要写入数据的文件流。

函数返回值

fwrite 函数的返回值是成功写入的数据项数量。

如果这个数量小于 nmemb,可能是因为发生错误或者文件结束(EOF)。

相关推荐
时空无限1 天前
linux mellanox 网卡队列 irq rps 相关
linux·运维
kdxiaojie1 天前
Linux 驱动研究 —— V4L2 (14)
linux·运维·笔记·学习
IMPYLH1 天前
HTML 的 <dl> 元素
linux·windows·html
如若1231 天前
Ubuntu 无 sudo 安装花生壳并实现 SSH 内网穿透:Conda 环境部署、冲突排查与自动启动
linux·运维·ubuntu·ssh·内网穿透
承渊政道1 天前
Linux系统学习【掌握Ext系列⽂件系统的相关内容】
linux·运维·学习·文件系统·存储结构·inode
Better Bench1 天前
WSL2 Ubuntu 中 Claude CLI “command not found” 故障排查与修复
linux·ubuntu·claude·wsl·claudecode
实心儿儿1 天前
Linux —— 进程间关系和守护进程
linux·运维·服务器
Dawn-bit1 天前
Linux磁盘管理详解
linux·运维·服务器·计算机网络·云计算
RisunJan1 天前
Linux命令-sftp(SSH 文件传输协议客户端)
linux·运维
龙仔7251 天前
人大金仓OS_Core数据库自动备份实施笔记(银河麒麟Linux)
linux·数据库·笔记·备份·人大金仓