第1关:HelloWorld
任务描述
题目描述:向文件in.txt中写入字符串HelloWorld。

#include<stdio.h>
void solve(){
/********** Begin *********/
    FILE *fp;
    fp=fopen("in.txt","w");
    fprintf(fp,"HelloWorld");
    fclose(fp);
/********** End **********/
}

第2关:文件读取和写入
任务描述
题目描述:从文件a.txt中读取三个整数,然后把这三个整数保存到b.txt中,两整数之间一个空格。

格式如下:
100 110 100

#include<stdio.h>
void solve(){
/********** Begin *********/
    int one,two,three;
    FILE *a,*b;
    a=fopen("a.txt","r");
    b=fopen("b.txt","w");
    int n=0;
    while(n!=1)
    {
        fscanf(a,"%d%d%d",&one,&two,&three);
        fprintf(b,"%d %d %d",one,two,three);
        n++;
    }
    fclose(a);
    fclose(b);
/********** End **********/
}

第3关:统计文本字母数量

任务描述
题目描述:读取a.txt中文本,统计文本中字母数量。

输入
读取a.txt读入文本
如:
abc abc

输出
输出文本中字母数量

样例输入
abc abc

样例输出
6

#include<stdio.h>
void solve(){
/********** Begin *********/
FILE *fp;
char word;
int num=0;
fp=fopen("a.txt","r");
while(!feof(fp))
{
    fscanf(fp,"%c",&word);
    if((word>='A'&&word<='Z')||(word>='a'&&word<='z'))
    {
        num++;
    }
}
printf("%d",num-1);
fclose(fp);
/********** End **********/
}

第4关:读取文件中指定学生信息
任务描述
题目描述:实现从文本中读取出指定学号的学生信息并显示,文本文件存放格式是每一行对应一个学生信息,最后一行没有换行符。

相关知识(略)
编程要求
根据提示,在右侧编辑器Begin-End处补充代码,完成本关要求。

测试说明
输入
solve(char s[])已经给一个字符串s,代表学生学号。
文件a.txt存放所有学生信息。

输出
输出该学号学生信息
如果不存在则输出Not Found!
#####样例输入
11405200102
a.txt中内容为:
11405200101 zhangsan 70 80 90 240 80
11405200102 lisi 80 60 70 210 70
#####样例输出
11405200102 lisi 80 60 70 210 70

#include<stdio.h>
#include<string.h>
void solve(char s[]){
/********** Begin *********/
    FILE *fp=fopen("a.txt","r");
    char num[12],stu[100];
    for(int i=0;i<10;i++)
    {
        fgets(num,12,fp);
        fseek(fp,-11*sizeof(char),1);//fseek中间的参量若为正值,则是正向移动,若为负值则为反向移动(及向后移动);
        fgets(stu,100,fp);
        if(strcmp(num,s)==0)
        {
            printf("%s",stu);
            break;
        }
        if(i==9)
        {
            printf("Not Found!");
        }
    }

/********** End **********/
}


Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐