File Programming - Examples to explain file reading and writing

Mainly explains file programming through the access of address book linked list

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

const char *path file path

const char *mode opening method or with what permissions

Open the file before reading and writing. The minefield here is that the format of reading must be strictly consistent with writing, otherwise it is extremely easy to make mistakes.
 

fscanf(file,"%s%s",newnode->num,newnode->name);

fprintf(file2,"%-11s %-15s\r\n",head->next->num,head->next->name);

Take a look at the main reading and writing code separately.

Here I use a while loop to put the read information into the linked list in the form of nodes.

Add:

Uncached I/O:        read, write, open...
Standard (cached) I/O:  fgets, fread, fwrite....

If we directly use non-cached I/O to read and write the kernel buffer, it will cause many troubles caused by poor management (such as too many writes at one time, or inefficiency caused by multiple system calls).

Standard (cached) I/O solves these problems for us. It handles many details, such as buffer allocation, performing I/O with optimized length, etc., making it easier for us to use.

Specific reference: Unbuffered IO and Buffered IO - From Rookie to Rookie - CSDN Blog https://blog.csdn.net/carolzhang8406/article/details/7227761

int fscanf_file(Linker *head)
{
    int flag;
    Linker *newnode,*p;
    p = head;
    
    FILE *file = fopen("./addressbook.txt","r");
    if(NULL == file)
    {
        perror("fopen file");
        return F;
    }
    
    fscanf(file,"%d",&flag);
    
    while(flag != 0)                                     //用尾插的方式放入读取的数据
    {
        p = head;
        
        newnode = (Linker *)malloc(sizeof(struct link)); //申请节点把读取的信息逐条放入
        if (newnode == NULL)
        {
            return F;
        }

        while(p->next != NULL)
        {
            p=p->next;
        }

        fscanf(file,"%s%s",newnode->num,newnode->name);
        fscanf(file,"%d",&flag);                          //读取每条信息编号

        newnode->next = NULL;
        p->next = newnode;

    }

    fclose(file);
    return T;
}

int fprint_file(Linker *head)
{
    
    int k = 1;
    FILE *file2 = fopen("./addressbook.txt","w+");//每次重新写入信息
    {
        if(file2 == NULL)
        {
            perror("fopen file2");
            exit(2);
        }
    }

    while(head->next != NULL)
    {
        fprintf(file2, "%d ",k++);   //为联系人编号
        fprintf(file2,"%-11s %-15s\r\n"
                ,head->next->num,head->next->name);
        head = head->next;
    }

    fprintf(file2, "%d\n",0);//标志位,如果只有0,没有联系人
    fclose(file2);

    return T;
}

The following is a very simple address book to assist in understanding and use 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define T 1
#define F 0

typedef struct link
{
    char num[15];        //学号
    char name[30];       //姓名
    struct link *next;
}Linker;

int fscanf_file(Linker *head);  //从文件夹读取信息
int fprint_file(Linker *head);  //把信息打印到文件

int insert_tail(Linker *head);  //插入联系人
int init(Linker **head);        //初始化
void print(Linker *head);       //显示

int main()
{
	Linker *head;
	init(&head);
	fscanf_file(head);
	insert_tail(head);
	fprint_file(head);
	print(head);
	
	
	return 0;
}

int init(Linker **head)
{
    Linker *newnode = (Linker *)malloc(sizeof(struct link));
    if (newnode == NULL)
    {
        return F;
    }

    newnode->next = NULL;
    *head = newnode;

    return T;
}

void print(Linker *head)
{
    int i = 0;
    if (head->next == NULL)
    {
        printf("无联系人信息!\n");
    }
    else
    {
        printf("\n\n");
        printf("联系人信息:\n\n");
        while(head->next != NULL)
        {
            printf("学号:%-11s 姓名:%-15s\n"
                ,head->next->num,head->next->name);
            head = head->next;
            i++;
        }
        printf("\n共有%d人\n",i);
    }
}

int insert_tail(Linker *head)
{
    Linker *p = head;
    Linker *q = head;
    int i;
    
    Linker *newnode = (Linker *)malloc(sizeof(struct link));
    if (newnode == NULL)
    {
        return F;
    }

    while(p->next != NULL)
    {
        p = p->next;
    }

    printf("\n请输入新建联系人的学号:\n");
    scanf("%s",newnode->num);
    while(strlen(newnode->num) != 11)
    {
        printf("请重新输入新建联系人的学号:\n");
        scanf("%s",newnode->num);
    }


    printf("\n请输入新建联系人的姓名:\n");
    scanf("%s",newnode->name);
    
    newnode->next = NULL;
    p->next = newnode;
    
    printf("\n*****************添加联系人成功!********************\n");
    
    return T;
}

int fscanf_file(Linker *head)
{
    int flag;
    Linker *newnode,*p;
    p = head;
    
    FILE *file = fopen("./addressbook.txt","r");
    if(NULL == file)
    {
        perror("fopen file");
        return F;
    }
    
    fscanf(file,"%d",&flag);
    
    while(flag != 0)
    {
        p = head;
        
        newnode = (Linker *)malloc(sizeof(struct link));
        if (newnode == NULL)
        {
            return F;
        }

        while(p->next != NULL)
        {
            p=p->next;
        }

        fscanf(file,"%s%s",newnode->num,newnode->name);
        fscanf(file,"%d",&flag);

        newnode->next = NULL;
        p->next = newnode;

    }

    fclose(file);
    return T;
}

int fprint_file(Linker *head)
{
    
    int k = 1;
    FILE *file2 = fopen("./addressbook.txt","w+");//每次重新写入信息
    {
        if(file2 == NULL)
        {
            perror("fopen file2");
            exit(2);
        }
    }

    while(head->next != NULL)
    {
        fprintf(file2, "%d ",k++);   //为联系人编号
        fprintf(file2,"%-11s %-15s\r\n"
                ,head->next->num,head->next->name);
        head = head->next;
    }

    fprintf(file2, "%d\n",0);//标志位,如果只有0,没有联系人
    fclose(file2);

    return T;
}

 

Guess you like

Origin blog.csdn.net/ls_dashang/article/details/82974400