c语言 文件写入和读取
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10
struct student{ /* 学生信息结构 */
char no[]; /* 学号 */
char name[]; /* 姓名 */
char sex[]; /* 性别 */
int score[]; /* 成绩和总分 */
}; int menu(); /* 菜单*/
void readsi(struct student stud[],int *n); /* 读取数据*/
void printsi(struct student *pstud,int n); /* 输出文件内容*/
void ssort(struct student *pstud, int n); /*按总分的降序排序函数 */
void xsort(struct student *pstud, int n);/*按学号的升序排序函数 */
void tjave(struct student *pstud, int n);/*统计各门功课的平均分函数 */
void tjrs(struct student *pstud,int n);/*统计男女同学的人数函数 */
void namesort(struct student *pstud,int n);/*按姓名升序排序 */
void math_excellent(struct student *pstud,int n);/*数学考试成绩优秀(>=90)*/
void all_excellent(struct student *pstud,int n);/*每门考试成绩优秀(>=90)或者总分》=270*/ void main() /* 主函数 */
{
struct student stud[N];
int code, n=;
readsi(stud,&n);
printf("\n 按<Enter>, 进入菜单: ");
scanf("%*c"); /* 暂停 */
while()
{
code=menu(); /* 调用主控菜单 */
switch(code)
{
case : exit();
case : printsi(stud,n);
printf("\n 按<Enter>, 进入菜单: ");
scanf("%*c");
break;
case : ssort(stud,n);break;
case : xsort(stud,n);break;
case : tjave(stud,n);break;
case : tjrs(stud,n);break;
case : namesort(stud,n);break;
case : math_excellent(stud,n);break;
case : all_excellent(stud,n);break; } scanf("%*c");
}
} int menu() /* 主控菜单函数 */
{
int code;
printf(" 菜 单\n");
printf(" *****************************************************\n");
printf(" 0. 退出 1. 显示学生信息 \n");
printf(" 2. 按总分排序 3. 按学号排序\n");
printf(" 4. 统计各门功课平均分 5. 统计男女同学人数\n");
printf(" 6. 按姓名排序 7. 数学考试成绩优秀人数\n");
printf(" 8. 考试成绩优秀人数 \n");
printf(" *****************************************************\n");
printf(" 请按序号选择:\n");
scanf("%d",&code);
return code;
} void readsi(struct student stud[],int *n) /* 读数据函数 */ //int *n;n需要返回
{
FILE *fp;
int i;
// if((fp=fopen("studf.txt","r"))==NULL)
if((fp=fopen("C:/Users/minmin/Desktop/studf.txt","r"))==NULL)//文件存放在指定路径,把路径写上就可以了
{
printf("Cannot open file!\n");
exit();
}
for(i=;!feof(fp);i++)
{
(*n)++;
fscanf(fp,"%s %s %s %d %d %d %d", stud[i].no,stud[i].name,stud[i].sex,
&stud[i].score[], &stud[i].score[], &stud[i].score[], &stud[i].score[]); stud[i].score[]=stud[i].score[]+stud[i].score[]+stud[i].score[];
}
fclose(fp);
} void printsi(struct student *pstud, int n) /* 输出数据函数 */
{
int i;
printf(" 学号 姓名 性别 数学 英语 C 总分\n");
printf("******************************************************\n");
for(i=;i<n;i++)
{
printf("%-8s %-8s %-2s %4d %4d %4d %4d\n", pstud[i].no,pstud[i].name,pstud[i].sex,
pstud[i].score[], pstud[i].score[], pstud[i].score[], pstud[i].score[]);
}
} void ssort(struct student *pstud,int n) /*按总分的降序排序函数 */
{
struct student temp;
int i,j,min;
for(i=;i<n;i++)
{
min=i; /* 找最小值元素的下标*/
for(j=i+;j<n;j++)
if(pstud[j].score[]>pstud[min].score[]) min=j;
if(min!=i) /* 交换 */
{
temp=pstud[i]; pstud[i]=pstud[min]; pstud[min]=temp;
}
}
} void xsort(struct student *pstud,int n) /*按学号的升序排序函数 */
{
struct student temp;
int i, j;
for(i=;i<n-;i++)
{
for(j=i+;j<n;j++)
{
if(strcmp(pstud[i].no,pstud[j].no)>)
{
temp=pstud[i];
pstud[i]=pstud[j];
pstud[j]=temp;
}
}
}
} void tjave(struct student *pstud, int n) /*统计各门功课的平均分函数 */
{
float avemath=,aveeng=,avec=,avesum=;
int i;
for(i=;i<n;i++)
{
avemath+=pstud[i].score[];
aveeng+=pstud[i].score[];
avec+=pstud[i].score[];
avesum+=pstud[i].score[];
}
avemath/=n; aveeng/=n; avec/=n; avesum/=n;
printf("共有%d个同学,各门功课及总分的平均分为:\n",n);
printf(" 数学 英语 C 总分\n");
printf("%5.2f %5.2f %5.2f %5.2f\n",avemath,aveeng,avec,avesum);
} void tjrs(struct student *pstud,int n) /*统计男女同学的人数函数 */
{
int i, nummen=, numwomen=;
for(i=;i<n;i++)
{
if(strcmp(pstud[i].sex,"男")==) nummen++;
else numwomen++;
}
printf(" 共有%d个同学: \n",n);
printf(" 其中男同学有%d个,女同学有%d个\n",nummen,numwomen);
} void namesort(struct student *pstud,int n)/*按姓名升序排序 */
{
struct student temp;
int i, j;
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
{
if(strcmp(pstud[i].name,pstud[j].name)>)
{
temp=pstud[i];
pstud[i]=pstud[j];
pstud[j]=temp;
}
}
}
} void math_excellent(struct student *pstud,int n)/*数学考试成绩优秀(>=90)*/
{
int i, num = ;
for(i=;i<n;i++)
{
if(pstud[i].score[]>=)
{
num++;
printf("%-8s %-8s %-2s %4d %4d %4d %4d\n", pstud[i].no,pstud[i].name,pstud[i].sex,
pstud[i].score[], pstud[i].score[], pstud[i].score[], pstud[i].score[]);
}
}
printf("数学优秀的人数为:%d\n",num);
} void all_excellent(struct student *pstud,int n)/*每门考试成绩优秀(>=90)或者总分》=270*/
{
int i, num = ;
for(i=;i<n;i++)
{
if(((pstud[i].score[]>=)&&(pstud[i].score[]>=)&&(pstud[i].score[]>=))||(pstud[i].score[]>=))
{
num++;
printf("%-8s %-8s %-2s %4d %4d %4d %4d\n", pstud[i].no,pstud[i].name,pstud[i].sex,
pstud[i].score[], pstud[i].score[], pstud[i].score[], pstud[i].score[]);
}
}
printf("优秀的人数为:%d\n",num);
}
c语言 文件写入和读取的更多相关文章
- Python学习笔记——文件写入和读取
1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ...
- 【PHP】文件写入和读取详解
文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...
- Java文件写入与读取实例求最大子数组
出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...
- PHP 文件写入和读取(必看篇)
文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...
- sql注入文件写入和读取
系统固定文件路径:https://blog.csdn.net/ncafei/article/details/54616826 /etc/passwd c:/windows/win.ini 文件读取使用 ...
- 从PCD文件写入和读取点云数据
(1)学习向PCD文件写入点云数据 建立工程文件ch2,然后新建write_pcd.cpp CMakeLists.txt两个文件 write_pcd.cpp : #include <iostr ...
- unity文件写入与读取
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; us ...
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...
- Java 通过 BufferReader 实现 文件 写入读取 示例
package com.javatest.techzero.gui; import java.io.BufferedReader; import java.io.File; import java.i ...
随机推荐
- vi所有特殊字符
vi5个特殊字符包含 /.^.$.*.. 在vi中用/查找时,()不做为特殊字符处理 比如:查找字符串(cyg_uint8 *)b 应该这样写 /(cyg_uint8 \*)b 只有 * 需要转义 \ ...
- webview相关链接
http://tech.it168.com/a2011/0517/1191/000001191561_2.shtml http://www.eoeandroid.com/thread-272495-1 ...
- 查看实时公网ip
icanhazip.com 使您在任何地方知道你的公网IP地址 icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com 使你在任何地方知道你的公网IP地址 ican ...
- [SQL]一个删选数据的例子,使用GROUP、DISTINCT
今天遇到的问题,纠结了一上午,终于解决了.在此记录下来,自我认为还有很多类似的问题都可以套用这段代码. 需求描述: 一个表MyImage,列有:号码ID,路径PATH 如: ID PATH 1 C ...
- #include <boost/regex.hpp>
boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...
- SpringNote01.基于SpringMVC-Hibernate的Blog系统
最近,在学习Spring,做这样一个简单的blog系统,主要是让自己动手练习使用Spring,熟练的使用才干进一步的深入学习.该项目使用Maven构建,使用git进行代码管理,通过这样一个小项目,熟悉 ...
- linux系统关机与重新启动命令
在linux下关机和重新启动系统有shutdown.halt.reboot.init,对于他们来说他们的内部工作过程是不同样的. 1.shutdown命令 使用它能够安全地关闭系统.然而在关闭系统时. ...
- ok6410驱动usb摄像头
为了做图像处理,须要用摄像头,搜到实验室仅仅有一个摄像头,是国安的.详细參数在终端中看到: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGluZzEyM ...
- 高性能浏览器网络(High Performance Browser Networking) 第二章
第2章 TCP篇 互联网的核心是两个协议,IP和TCP. IP也叫Internet协议,提供主机到主机的路由和寻址:TCP,传输控制协议,在不可靠的传输通道上提供一个可靠的网络抽象.TCP / IP协 ...
- js仿百度文库文档上传页面的分类选择器_第二版
仿百度文库文档上传页面的多级联动分类选择器第二版,支持在一个页面同一时候使用多个分类选择器. 此版本号把HTML,CSS,以及图片都封装到"category.js"中.解决因文件路 ...