C language file reading and writing

1. Read and write txt files

This code can write a string to a file, and read a string from a file and output it to the console.
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
int main()
{
    FILE *fp;
    char str[100];
    // 写入文件
    fp = fopen("test.txt", "w");
    fprintf(fp, "This is a test.\n");
    fputs("Hello, World!", fp);
    fclose(fp);
    // 读取文件
    fp = fopen("test.txt", "r");
    while (fgets(str, 100, fp) != NULL)
    {
        printf("%s", str);
    }
    fclose(fp);
    return 0;
}

2. Read and write binary files

First open a file named "data.bin", write the string "Hello, world!" to the file using the fwrite() function, and then close the file. Then open the file again, use the fread() function to read the data in the file into the buffer, and finally output the content in the buffer.
#include <stdio.h>
int main() {
    FILE *fp;
    char buffer[100];
    // 写入二进制文件
    fp = fopen("data.bin", "wb");
    fwrite("Hello, world!", sizeof(char), 13, fp);
    fclose(fp);
    // 读取二进制文件
    fp = fopen("data.bin", "rb");
    fread(buffer, sizeof(char), 13, fp);
    fclose(fp);
    printf("%s\n", buffer);
    return 0;
}

3. Read and write structure

A structure called student is defined , which contains the student's name, age and grade. In the main function, we first create a variable stu of type student and initialize its value. Then, we use the fopen function to open a file named student.dat , and use the fwrite function to write stu into the file. Next, we use the fopen function to open the file again, and use the fread function to read data from the file into the stu variable. Finally, we print out the values ​​in the stu variable, which is the student's name, age, and grade.
Note that this code is just an example, and it needs to be modified and improved according to specific needs in actual applications.

Basic

#include <stdio.h>
struct student
{
    char name[20];
    int age;
    float score;
}
;
int main() {
    struct student stu =
    {
        "Tom", 18, 90.5
    }
    ;
    FILE *fp;
    // 写入结构体数据到文件
    fp = fopen("student.dat", "wb");
    fwrite(&stu, sizeof(struct student), 1, fp);
    fclose(fp);
    // 从文件中读取结构体数据
    fp = fopen("student.dat", "rb");
    fread(&stu, sizeof(struct student), 1, fp);
    printf("Name: %s\nAge: %d\nScore: %.1f\n", stu.name, stu.age, stu.score);
    fclose(fp);
    return 0;
}

Improvement

#include <stdio.h>
typedef struct
{
    int id;
    char name[20];
    float score;
}Student;

int main()
{
    Student stu =
    {
        1, "Tom", 89.5
    };
    FILE *fp = fopen("student.dat", "wb");
    if (fp == NULL)
    {
        printf("Failed to open file.\n");
        return 1;
    }
    fwrite(&stu, sizeof(Student), 1, fp);
    fclose(fp);
    fp = fopen("student.dat", "rb");
    if (fp == NULL)
    {
        printf("Failed to open file.\n");
        return 1;
    }
    fread(&stu, sizeof(Student), 1, fp);
    printf("ID: %d\nName: %s\nScore: %.1f\n", stu.id, stu.name, stu.score);
    fclose(fp);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_42815643/article/details/129539716