day10 C语言 顺序读写

C语言

顺序读写

fputs fgets

#include<stdio.h>
#include<stdlib.h>
#define MAX 1024

int main (void)
{
    FILE *fp;

    char buffer[MAX];

    if((fp = fopen("lines.txt","w")) == NULL)
    {
        printf("打开文件失败!\n");
        exit(EXIT_FAILURE);
    }

    fputs("Line one :I Love The World\n",fp);
    fputs("Line two :I Love Yoona\n",fp);
    fputs("Line three :Yoona == The World",fp);
    //读取到'\n'或者EOF都视为一个字符串
    //最后一行也有'\n'就会重复打印

    fclose(fp);

    if((fp = fopen("lines.txt","r")) == NULL)
    {
        printf("打开文件失败!\n");
        exit(EXIT_FAILURE);
    }

    //feof遇到EOF则返回1
    while(!feof(fp))
    {
        fgets(buffer,MAX,fp);
        printf("%s",buffer);

    }
    return 0;
}

fprint fscanf

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

int main (void)
{
    FILE *fp;
    struct tm *p;
    time_t t;

    time(&t);
    p = localtime(&t);

    if((fp = fopen("date.txt","w")) == NULL)
    {
        printf("打开文件失败!\n");
        exit(EXIT_FAILURE);
    }

    fprintf(fp,"%d-%d-%d",1900 + p->tm_year,1 + p->tm_mon, p->tm_mday);

    fclose(fp);

    int year, month, day;

    if((fp = fopen("date.txt","r")) == NULL)
    {
        printf("打开文件失败!\n");
        exit(EXIT_FAILURE);
    }

    fscanf(fp,"%d-%d-%d", &year, &month, &day);
    printf("%d-%d-%d\n", year, month, day);

    fclose(fp);

    return 0;
}

fwrite fread

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

struct Date
{
    int year;
    int month;
    int day;
};

struct Book
{
    char name[40];
    char author[40];
    char publisher[40];
    struct Date date;

};
int main (void)
{
    FILE *fp;
    struct Book *book_for_write, *book_for_read;

    book_for_write = (struct Book *)malloc(sizeof(struct Book));
    book_for_read = (struct Book *)malloc(sizeof(struct Book));

    if(book_for_write == NULL || book_for_read ==NULL)
    {
        printf("内存分配失败! \n");
        exit(EXIT_FAILURE);
    }

    strcpy(book_for_write->name, "《带你学C带你飞》");
    strcpy(book_for_write->author, "Yoona");
    strcpy(book_for_write->publisher, "清华大学出版社");
    book_for_write->date.year = 2021;
    book_for_write->date.month = 1;
    book_for_write->date.day = 22;

    if((fp = fopen("file.txt","w")) == NULL)
    {
        printf("打开文件失败!\n");
        exit(EXIT_FAILURE);
    }

    fwrite(book_for_write,sizeof(struct Book),1,fp);
    fclose(fp);

    if((fp = fopen("file.txt","r")) == NULL)
    {
        printf("打开文件失败!\n");
        exit(EXIT_FAILURE);
    }

    fread(book_for_read, sizeof(struct Book),1,fp);
    printf("书名:%s\n",book_for_read->name);
    printf("作者:%s\n",book_for_read->author);
    printf("出版社:%s\n",book_for_read->publisher);
    printf("出版日期:%d-%d-%d\n",book_for_read->date.year, book_for_read->date.month, book_for_read->date.day);

    fclose(fp);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追寻远方的人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值