#include<stdio.h> int main() { int c=0; char ch; FILE* fp; fp = fopen("data4.txt", "r"); if (fp == NULL) { printf("无法打开文件\n"); return 1; } ch = fgetc(fp); while (ch != EOF) { if (ch != ' ') { c++; } ch = fgetc(fp); } printf("%d", c-1); fclose(fp); return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 10 typedef struct { long int id; char name[20]; float objective; // 客观题得分 float subjective; // 操作题得分 float sum; char level[10]; } STU; // 函数声明 void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分,操作题得分\n", N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息, 并保存到文件中"); output(stu, N); return 0; } // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 void input(STU s[], int n) { int i; FILE* fin; fin = fopen("examinee.txt", "r"); if (fin == NULL) { printf("fail to open file\n"); exit(0); } while (!feof(fin)) { for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective); } fclose(fin); } // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 // 不仅输出到屏幕上,还写到文本文件result.txt中 void output(STU s[], int n) { FILE* fout; int i; // 输出到屏幕 printf("\n"); printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); for (i = 0; i < n; i++) printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level); // 保存到文件 fout = fopen("result.txt", "w"); if (!fout) { printf("fail to open or create result.txt\n"); exit(0); } fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); for (i = 0; i < n; i++) fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level); fclose(fout); } // 对考生信息进行处理:计算总分,排序,确定等级 void process(STU s[], int n) { // 补足代码 int i,z,p,a,b,k; STU t; for (i = 0; i < n; i++) { s[i].sum = (s[i].objective) * 0.4 + (s[i].subjective) * 0.6; } for (z = 0; z < n - 1; z++) { for (p = 0; p < n - 1; p++) { if (s[p].sum < s[p + 1].sum) { t = s[p]; s[p] = s[p + 1]; s[p + 1] = t; } } } a = n / 10; b = n / 2; for (k = 0; k < n; k++) { if ((k + 1) <= a) { strcpy(s[k].level, "优秀"); } if ((k + 1)>a&&(k+1)<=b) { strcpy(s[k].level, "合格"); } if ((k + 1) > b) { strcpy(s[k].level, "不合格"); } } }
#include<stdio.h> #include<stdlib.h> typedef struct { char num[100],name[10],cl[20]; }STU; int main() { int i,p,z; FILE* fp; fp = fopen("list.txt", "r"); STU s[80]; if (fp == NULL) { printf("无法打开文件"); } for (i = 0; i < 80; i++) { fscanf(fp, "%s %s %s", s[i].num, s[i].name, s[i].cl); } fclose(fp); fp = fopen("lucky.txt", "w"); if (fp == NULL) { printf("无法打开文件"); } for (p = 0; p < 5; p++) { z = rand() % 80; fprintf(fp, "%s\t%s\t%s\n", s[z].num, s[z].name, s[z].cl); } }
标签:fp,得分,int,STU,实验,printf,t% From: https://www.cnblogs.com/ak47ss/p/17008536.html