码迷,mamicode.com
首页 > 编程语言 > 详细

用C语言读写数据

时间:2017-06-12 22:14:19      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:null   i++   ==   fopen   txt   har   abi   include   read   

//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\n");
		scanf("%s\t%d\t%d\t%d\t%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno);
		printf("Average of %s is:%d\n",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\n");
    FILE *fd=fopen("data.txt","w");
	if (fd==NULL)
	   return 0;
	while (i<5)
	{
	   scanf("%s\t%d\t%d\t%d\t%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno);
       fprintf(fd,"%s\t%d\t%d\t%d\t%ld\n",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\t%d\t%d\t%d\t%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno)!=EOF)
	{
		printf("%s\t%d\t%d\t%d\t%ld\n",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\t%d\t%d\t%d\t%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno)!=EOF)
	{
		printf("%s\t%d\t%d\t%d\t%ld\n",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\n");
    FILE *fd=fopen("databin","wb");
	if (fd==NULL)
		return 0;
	while (i<5)
	{
		scanf("%s\t%d\t%d\t%d\t%ld",&stu[i].name,&stu[i].subject1,&stu[i].subject2,&stu[i].subject3,&stu[i].sno);
		fprintf(fd,"%s\t%d\t%d\t%d\t%ld\n",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);

}

  

用C语言读写数据

标签:null   i++   ==   fopen   txt   har   abi   include   read   

原文地址:http://www.cnblogs.com/ewitt/p/6995011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!