IO进程 day1

思维导图

1.学生信息写入

存在如下结构体

struct Student{
    char name[16];
    int age;
    double math_score;
    double chinese_score;
    double english_score;
    double physics_score;
    double chemistry_score;
    double bio_score;
};
申请该结构体数组,容量为5,初始化5个学生的信息
使用fprintf将数组中的5个学生信息,保存到文件中去
下一次程序运行的时候,使用fscanf,
将文件中的5个学生信息,写入(加载)到数组中去,并直接输出学生信息//???
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>                                                  
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct Student
{
    char name[16];
    int age;
    double math;
    double chinese;
    double english;
    double physic;
    double chemistry;
    double biological;
}student;
int main(int argc, const char *argv[])
{
    student stu[5]={{"ikun",18,100,100,100,100,100,100},
        {"cxk",19,90,90,90,90,90,90},
        {"mjq",20,80,80,80,80,80,80},
        {"wzy",21,70,70,70,70,70,70},
        {"fcc",22,66,66,66,66,66,66}
    };
    FILE *fp=fopen("./1.txt","w");
    if(fp==NULL)
    {
        perror("fopen");
        return 1;
    }
    for(int i=0;i<5;i++)
    {
        fprintf(fp,"student%d:\n",i+1);
        fprintf(fp,"name:%s\n",stu[i].name);
        fprintf(fp,"age:%d\n",stu[i].age);
        fprintf(fp,"math score:%.2lf\n",stu[i].math);
        fprintf(fp,"chinese score:%.2lf\n",stu[i].chinese);
        fprintf(fp,"english score:%.2lf\n",stu[i].english);
        fprintf(fp,"physic score:%.2lf\n",stu[i].physic);
        fprintf(fp,"chemistry score:%.2lf\n",stu[i].chemistry);
        fprintf(fp,"biological score:%.2lf\n",stu[i].biological);
        fprintf(fp,"\n");
    }
    fclose(fp);
    fp=fopen("./1.txt","r");
    if(fp==NULL)
    {
        perror("fopen");
        return 1;
    }
    char str[99999];
    int i=0;
    while(1)
    {
         char retval=fscanf(fp,"%c",str+i);
         if(retval==EOF)
         {
             break;
         }
         i++;
    }
    fclose(fp);
    int len=strlen(str);
    for(int j=0;j<len;j++)
    {
        printf("%s\n",str+j);
    }
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值