zoukankan      html  css  js  c++  java
  • 用C语言读写数据

    //1-5题
    #include "stdio.h"
    typedef struct 
    {
       char name[10];//姓名
       int subject1,subject2,subject3;//科目1,2,3
       long int sno;//学号
    }student;
    int InputTerm(student *stu)//在终端输入数据并每人的计算平均分
    {
      	int i=0;
    	while (i<5)
    	{
    		printf("Pls input name,subject1,subject2,subject3 and student No
    ");
    		scanf("%s	%d	%d	%d	%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno);
    		printf("Average of %s is:%d
    ",stu[i].name,(stu[i].subject1,stu[i].subject2,stu[i].subject3)/3); 
    		i++;
    	}
    	return 1;
    }
    
    int InputDataTxt(student *stu)//输出到txt文件
    {
    	int i=0;
    	printf("Input 5 student score
    ");
        FILE *fd=fopen("data.txt","w");
    	if (fd==NULL)
    	   return 0;
    	while (i<5)
    	{
    	   scanf("%s	%d	%d	%d	%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno);
           fprintf(fd,"%s	%d	%d	%d	%ld
    ",stu[i].name,stu[i].subject1,stu[i].subject2,stu[i].subject3,stu[i].sno); 
    	   i++;
    	}
    	fclose(fd);
    	return 1;
    }
    int ReadData(student *stu)//从文本文件中读数据
    {
    	FILE *fd=fopen("data.txt","r");
    	if (fd==NULL)
    		return 0;
    	int i=0;
        while(fscanf(fd,"%s	%d	%d	%d	%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno)!=EOF)
    	{
    		printf("%s	%d	%d	%d	%ld
    ",stu[i].name,stu[i].subject1,stu[i].subject2,stu[i].subject3,stu[i].sno);
            i++;
    	}
    	fclose(fd);
        return 1;
    }
    int ReadDataBin(student *stu)//从二进制文件中读数据
    {
    	FILE *fd=fopen("databin","rb");
    	if (fd==NULL)
    		return 0;
    	int i=0;
        while(fscanf(fd,"%s	%d	%d	%d	%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno)!=EOF)
    	{
    		printf("%s	%d	%d	%d	%ld
    ",stu[i].name,stu[i].subject1,stu[i].subject2,stu[i].subject3,stu[i].sno);
            i++;
    	}
    	fclose(fd);
        return 1;
    }
    int InputDataBin(student *stu)//输入到二进制文件
    {
    	int i=0;
    	printf("Input 5 student score
    ");
        FILE *fd=fopen("databin","wb");
    	if (fd==NULL)
    		return 0;
    	while (i<5)
    	{
    		scanf("%s	%d	%d	%d	%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno);
    		fprintf(fd,"%s	%d	%d	%d	%ld
    ",stu[i].name,stu[i].subject1,stu[i].subject2,stu[i].subject3,stu[i].sno); 
    		i++;
    	}
    	fclose(fd);
    	return 1;
    }
    
    
    void main()
    {
    	student stu[5];
    	InputTerm(stu);
        //InputData(stu);
    	//ReadData(stu);
    	//InputDataBin(stu);
    	//ReadDataBin(stu);
    
    }
    

      

  • 相关阅读:
    怎样在过滤器中读取配置信息?
    怎样将直接数据库中Json字段,映射到Mybatis中的Map类型?
    spring/boot 打包,资源/配置/业务文件分离
    使用VS Code推送代码到GitHub
    Clion下jni配置
    curl post请求总是返回417错误
    ubuntu 12.10 apt-get 源
    如何让git小乌龟工具TortoiseGit记住你的账号密码
    FastCgi与Cgi
    Libevent核心原理
  • 原文地址:https://www.cnblogs.com/ewitt/p/6995011.html
Copyright © 2011-2022 走看看