1.从键盘输入一个字符串,将其中的小写字母全部转化成大写字母,然后输出到一个磁盘文件test中保存.输入的字符以"!"结束
需要在代码规定的地方新建一个文件,我这里是在桌面新建的 test.txt
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void main() {
FILE *fp;
char str[50];
if (!(fp = fopen("C:\\Users\\winuser\\Desktop\\test.txt","w"))) {//打开文件并写入
printf("打开文件失败!");
}
printf("输入一个字符串:\n");
gets(&str);
int len = strlen(str);
int i = 0;
while(str[i]!= '!') {
if (str[i]>='a' && str[i]<='z') {
str[i] = str[i] - 32;
}
fputc(str[i], fp);//将str数据写入文件
i++;
}
fclose(fp);//写入完毕,关闭文件
fp = fopen("C:\\Users\\winuser\\Desktop\\test.txt", "r");//打开文件并读取
fgets(str, len+1, fp);//读取数据
printf("%s\n", str);
fclose(fp);//读取完毕,关闭文件
}
2.有两个磁盘文件"A"和"B",各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件"C"中去.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void main() {
FILE *fpa;
FILE *fpb;
FILE *fpc;
char stra[20],strb[20],str,strc[50],temp;
int n;
//在磁盘文件A中存放一行代码
fpa = fopen("A","w");
printf("请输入磁盘文件A的一行字母:\n");
gets(&stra);
fputs(stra,fpa);
fclose(fpa);
//在磁盘文件B中存放一行代码
fpb = fopen("B", "w");
printf("请输入磁盘文件B的一行字母:\n");
gets(&strb);
fputs(strb, fpb);
fclose(fpa);
//打开磁盘文件A进行读取
int len = strlen(stra);
fpa = fopen("A", "r");
//打开磁盘文件B进行读取
int len2 = strlen(strb);
fpb = fopen("B", "r");
//将两个文件信息合并
int i;
for (i = 0; (str = fgetc(fpa)) != EOF;i++) {//从fpa中一个一个读取,直到结束
strc[i] = str;
}
int i2 = i;
for (i2 = i; (str = fgetc(fpb)) != EOF; i++) {//在上个i的基础上就行读取
strc[i] = str;
}
strc[i] = '\0';
fclose(fpa);
fclose(fpb);
n = i;
//将合并之后的字母进行排序
for (int j = 0; j < n;j++) {
for (int k = j + 1; k < n;k++) {
if(strc[j]>strc[k]){
temp = strc[j];
strc[j] = strc[k];
strc[k] = temp;
}
}
}
//将排好序的字母存放到磁盘C中
fpc = fopen("C","w");
fputs(strc, fpc);
fclose(fpc);
//将磁盘C的字母输出
printf("\n输出磁盘C的数据:\n");
fpc = fopen("C", "r");
for (int i = 0; (str = fgetc(fpc)) != EOF; i++) {
printf("%c", str);
}
fclose(fpc);
}
3.有5个学生,每个学生有3门课程的成绩,从键盘输入学生数据(包括学号、姓名、3门课程成绩),
计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件’stud’中.
#define _CRT_SECURE_NO_WARNINGS
#define N 5
#include <stdio.h>
struct student
{
int stuId;
char name[10];
float score[3];
float average;
}stu[N];
void main() {
void input(struct student stu[]);
void output(struct student stu[]);
input(stu);
output(stu);
//存
FILE *fp;
fp = fopen("stud","w");
for (int i = 0; i < N;i++) {
if (fwrite(&stu[i],sizeof(struct student),1,fp)!=1) {
printf("文件写入失败...");
}
}
fclose(fp);
//验证 取
fp = fopen("stud", "r");
for (int i = 0; i < N;i++) {
fread(&stu[i],sizeof(struct student),1,fp);
printf("\n%d,%s,%5.2f,%5.2f,%5.2f,%5.2f\n",stu[i].stuId,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].average);
}
}
void input(struct student stu[]){
for (int i = 0; i < N;i++) {
printf("请输入第 %d 个学生的数据:\n",i+1);
printf("学号:");
scanf("%d",&stu[i].stuId);
printf("姓名:");
scanf("%s",&stu[i].name);
for (int j = 0; j < 3;j++) {
printf("课程 %d:",j+1);
scanf("%f",&stu[i].score[j]);
}
printf("\n");
}
}
void output(struct student stu[]) {
printf("学号 姓名 课程1 课程2 课程3 平均分\n");
printf("----------------------------------------------\n");
float sum;
for (int i = 0; i < N; i++) {
printf("%d ", stu[i].stuId);
printf("%s ", stu[i].name);
sum = 0;
for (int j = 0; j < 3; j++) {
printf("%5.2f ", stu[i].score[j]);
sum += stu[i].score[j];
}
stu[i].average = sum / 3;
printf("%5.2f", stu[i].average);
printf("\n");
}
}
4.在上题“stud”文件中的学生数据,按平均分进行排序处理,将已排序的学生数据存入一个新文件“stu-sory”中.
#define _CRT_SECURE_NO_WARNINGS
#define N 5
#include <stdio.h>
#include <string.h>
struct student
{
int stuId;
char name[10];
float score[3];
float average;
}stu[N],temp;
void main() {
FILE *fp;
fp = fopen("stud", "r");
for (int i = 0; i < N;i++) {
fread(&stu[i],sizeof(struct student),1,fp);
printf("\n%d,%s,%5.2f,%5.2f,%5.2f,%5.2f\n",stu[i].stuId,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].average);
}
fclose(fp);
//将排序后的数据存放到stu-sory中
fp = fopen("stu-sory","w");
//排序
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (stu[i].average > stu[j].average) {
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
printf("\n排序之后的数据:\n");
for (int i = 0; i < N;i++) {
fwrite(&stu[i],sizeof(struct student),1,fp);
printf("\n%d,%s,%5.2f,%5.2f,%5.2f,%5.2f\n", stu[i].stuId, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].average);
}
fclose(fp);
}