task4
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h> #include<stdio.h> #define N 100 int main() {FILE *fp; char n,*p; int count=0; fp=fopen("data4.txt","r"); while(feof(fp)==0) { n=fgetc(fp); if(n!=' '&&n!=' '&&n!='\n') {count++;} } count--; printf("%d",count); system("pause"); return 0; }
task5
// 把通过考试的考生完整信息写入文件list_pass.txt // 准考证号,姓名,客观题得分,操作题得分,总分,结果 void foutput(STU s[], int n) { FILE *fout; int i; // 保存到文件 fout = fopen("list_pass.txt", "w"); if (!fout) { printf("fail to open or create list_pass.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].ans); fclose(fout); } // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 int process(STU st[], int n, STU st_pass[]) { int i,k=0; for(i=0;i<n;i++) { st[i].sum=st[i].objective+st[i].subjective; if(st[i].sum>=60) {strcpy(st[i].ans,"通过"); st_pass[k].id=st[i].id; strcpy(st_pass[k].name,st[i].name); strcpy(st_pass[k].ans,st[i].ans); st_pass[k].objective=st[i].objective; st_pass[k].subjective=st[i].subjective; st_pass[k].sum=st[i].sum; k++; } else strcpy(st[i].ans,"不通过"); } return k; }
task6
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
struct stu
{
long long int id;
char name[20];
char information[40];
}st[80];
int main()
{
int i,x;
FILE *fp;
FILE *p;
p=fopen("lucky.txt","w");
fp=fopen("list.txt","r");
for(i=0;i<80;i++)
{
fscanf(fp,"%lld %s %s",&st[i].id,st[i].name,st[i].information);
}
for(i=1;i<=5;i++)
{
srand(time(0)*i);
x=rand()%80;
printf("%lld %-7s %-10s\n",st[x].id,st[x].name,st[x].information);
fprintf(p,"%lld %-7s %-10s\n",st[x].id,st[x].name,st[x].information);
}
fclose(fp);
fclose(p);
system("pause");
return 0;
}
标签:fp,文件,name,int,编程,st,实验,pass,id From: https://www.cnblogs.com/lvys/p/17917837.html