用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\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);

}

  

时间: 2024-08-19 17:53:18

用C语言读写数据的相关文章

R语言读写中文编码方式

最近遇到一个很头疼的事,就是 R语言读写中文编码方式.在网上找到了一篇博文,谢谢博主的精彩分享,让我很快解决了问题,在此也分享一下 R语言读写数据的方法很多,这里主要是我在使用read.csv/read.table和write.csv/write.table时遇到的一些中文格式编码的问题.常见的中文编码方式两种:GBK(GB2312)和UTF-8.     Windows系统下: read.csv()和read.table()方法不指定文件格式时,默认读取的文件是GBK格式.Rstudio里面有

ADO.NET学习笔记-读写数据

1. DbCommand对象 要读写数据,一是需要有效的数据链接,二是需要DbCommand对象向数据源传递SQL命令.DbCommand对象包含的命令可以是DML数据操作语言,也可以是DDL数据定义语言.创建DbCommand的最好方式是先创建DbConnection,然后使用DbConnection的CreateCommand()方法,这样创建的DbCommand与DbConnection之间最为匹配.DbCommand必备的两个重要属性为CommandText和CommandType,示例

.net环境下跨进程、高频率读写数据

原文:.net环境下跨进程.高频率读写数据 一.需求背景 1.最近项目要求高频次地读写数据,数据量也不是很大,多表总共加起来在百万条上下. 单表最大的也在25万左右,历史数据表因为不涉及所以不用考虑, 难点在于这个规模的热点数据,变化非常频繁. 数据来源于一些检测设备的采集数据,一些大表,有可能在极短时间内(如几秒钟)可能大部分都会变化, 而且主程序也有一些后台服务需要不断轮询.读写某种类型的设备,所以要求信息交互时间尽可能短. 2.之前的解决方案是把所有热点数据,统一加载到共享内存里边,到也能

01. SQL Server 如何读写数据

原文:01. SQL Server 如何读写数据 一. 数据读写流程简要SQL Server作为一个关系型数据库,自然也维持了事务的ACID特性,数据库的读写冲突由事务隔离级别控制.无论有没有显示开启事务,事务都是存在的.流程图如下: 数据读写流程图 0. 事务开始(1) 所有DML语句必然是基于事务的,如果没有显式开启事务,即手动写下BEGIN TRAN,SQL Server则把每条语句作为一个事务,并自动提交事务. 也就是说SQL SERVER 默认不开启隐式事务,这点与ORACLE正好相反

inputstream和outputstream读写数据模板代码

//读写数据模板代码 byte buffer[] = new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(buffer,0,len); }

读写数据块函数--fread( )和fwrite( )函数

读写数据块函数--fread( )和fwrite( )函数    调用方式分别为:    (1) .fread(buffer,size,count,fp): 该函数将fp指向的文件的数据以数据块的形式读入内存buffer    (2).write(buffer,size,count,fp):该函数将内存buffer的内容以数据块的形式写入fp指向的文件.    其中:buffer:是一个指针.对于fread来说 它是读入数据的有效地址.对 fwrite来说,是要写盘的数据地址(起始地址). si

【转】iphone 输入/输出流异步读写数据

原文:iphone 输入/输出流异步读写数据 分类: iphone2012-05-30 14:50 2484人阅读 评论(1) 收藏 举报 iphoneattributesinterfacepathstreamfile 1.首先是往文件里写入数据 WriteFile.h [cpp] view plaincopy #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @class NoteDb; @interface Wr

如何选择容器,主要从存放要求和读写数据效率两方面考虑

1 ,存放要求 无序:set,不能重复. 有序:List,允许重复 “key-value”对:Map 2 ,读写效率 Hash:两者都最高 Array:读快改慢 Linked:读慢改快 Tree:加入元素可排序使用 如何选择容器,主要从存放要求和读写数据效率两方面考虑,布布扣,bubuko.com

R语言进行数据预处理

R语言进行数据预处理wranging li_volleyball 2016年3月22日 data wrangling with Rpackages:tidyr dplyr Ground rules library(tidyr) library(dplyr) ## ## Attaching package: 'dplyr' ## The following objects are masked from 'package:stats': ## ## filter, lag ## The follo