文件练习题

1.从键盘输入一个字符串,将其中小写字母全部换成大写字母,然后输入到一个磁盘文件test中保存。输入字符串以“!”结束

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	 FILE*fp;
	 char arr[100],p;
	 int i,j,k;
	 
	 fp = fopen("A.txt","r");
	 while(!feof(fp))
	 {
	 	fscanf(fp,"%s",arr);
	 }
	 fclose(fp);
	 
	 fp = fopen("B.txt","r");
	 while(!feof(fp))
	 {
	 	fscanf(fp,"%s",&arr[strlen(arr)]);
	 }
	 fclose(fp);
	 
	 for(i=0;i<strlen(arr)-1;i++)
	 {
	 	k=i;
	 	for(j=i+1;j<strlen(arr);j++)
	 	{
	 		if(arr[k]>arr[j])
			 k = j;
			if(k!=i)
			{
				p=arr[i];
				arr[i]=arr[k];
				arr[k]=p;
			 } 
		 }
	 }
	 
	 fp = fopen("c.txt","w");
	 fputs(arr,fp);
	 fclose(fp);
	 
	 return 0;
}

2. 有两个磁盘文件A和B,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到新文件C中

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int main()
{
    FILE *fp;
    int i;

    struct student
    {
        int num;
        char name[20];
        float score1;
        float score2;
        float score3;
        float ave;
    }stu[SIZE];

    //输入5个学生信息
    printf("请依次输入5个学生的学号、姓名、三门课程成绩:\n");
    for(i=0;i<SIZE;i++)
    {
        printf("No.");
        scanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score1,&stu[i].score2,&stu[i].score3);
        stu[i].ave=(stu[i].score1+stu[i].score2+stu[i].score3)/3.0;
    }

    //将学生信息写入文件stud.dat中
    if((fp=fopen("stud.dat","w"))==NULL)
    {
        printf("该文件打开失败!");
        exit (0);
    }    
    for(i=0;i<SIZE;i++)
    {
        fprintf(fp,"%d\t",stu[i].num);
        fprintf(fp,"%s\t",stu[i].name);
        fprintf(fp,"%.2f\t%.2f\t%.2f\t%.2f",stu[i].score1,stu[i].score2,stu[i].score3,stu[i].ave);
        if(i!=4) fputs("\n",fp);
    }
    fclose(fp);

    //再从文件stud.dat文件中读取数据并打印至屏幕
    if((fp=fopen("stud.dat","r"))==NULL)
    {
        printf("该文件打开失败!");
        exit (0);
    }
    i=0;
    putchar(10);
    putchar(10);
    while(!feof(fp))
    {
        fscanf(fp,"%d",&stu[i].num);
        fscanf(fp,"%s",stu[i].name);
        fscanf(fp,"%f%f%f%f",&stu[i].score1,&stu[i].score2,&stu[i].score3,&stu[i].ave);
        printf("NO.%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].ave);
    }
    fclose(fp);

    return 0;
}

 3.有5个学生,每个学生有3门课的成绩,从键盘输入学生数据(学号,姓名,3门课程成绩),计算平均成绩,将原有数据和平均分数放入磁盘文件stud

#include<stdio.h>

int main()
{
	int i,flag;
    char str[80],c;
    FILE *fp;
    fp=fopen("text.dat","w");
    flag=1;
    while(flag==1)
	{
		printf("Input string:");
        gets(str);
        fprintf(fp,"%s",str);//把字符串str按%s的格式输出到fp指向的文件text中 
        printf("输入Y或y继续循环,输入其他字符结束循环:");
        c=getchar();
        getchar();//吸收回车产生的换行符 
        if((c=='Y')||(c=='y'))
        	flag=0;
        printf("\n");
	}
    fclose(fp);
    fp=fopen("text.dat","r");
    printf("转换后的字符串为:");
    while(fscanf(fp,"%s",str)!=EOF)//从fp指向的磁盘文件text中读取字符串送给字符串str 
	{
		for(i=0;str[i]!='\0';i++)
		{
			if((str[i]>='a')&& (str[i]<='z'))//若字符串中有小写字母 
        		str[i]=str[i]-32;//就把字符串中每个小写字母变成大写字母 
		}
        printf("%s",str);
	}
   fclose(fp);
   return 0;
}

 4.从键盘输入若干行字符(每行长度不一样),输入后把它们存储到一个磁盘文件里,再从该文件中读入这些数据,将其中的小写字母转换成大写字母后在显示屏上输出

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
#define LINES    2
 
void main()
{
    int i;
    
    //将输入的内容保存到文件
    char s[10][100];
    for(i = 0; i < LINES; i++)
    {
        gets(s[i]);
    }
 
    FILE *fp;
    char *filename1 = "temp.txt";
    if((fp = fopen(filename1, "w")) == NULL)
    {
        printf("Open File %s Failed!\n", filename1);
    }
 
    for(i = 0; i < LINES; i++)
    {
        fputs(s[i], fp);
    }
 
    fclose(fp);
 
    //从文件中将字符读取并将小写字母转化为大写字母输出
    if((fp = fopen(filename1, "r")) == NULL)
    {
        printf("Open File %s Failed!\n", filename1);
    }
 
    char temp[100 * LINES];
    fgets(temp, 100 * LINES, fp);
 
    for(i = 0; temp[i] != '\0'; i++)
    {
        if(temp[i] >= 'a' && temp [i] <= 'z')
        {
            temp[i] = temp[i] - 'a' + 'A';
        }
    }
 
    printf("%s\n", temp);
 
    fclose(fp);
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
#define LINES    2
 
void main()
{
    int i;
    
    
    char s[10][100];
    for(i = 0; i < LINES; i++)
    {
        gets(s[i]);
    }
 
    FILE *fp;
    char *filename1 = "temp.txt";
    if((fp = fopen(filename1, "w")) == NULL)
    {
        printf("Open File %s Failed!\n", filename1);
    }
 
    for(i = 0; i < LINES; i++)
    {
        fputs(s[i], fp);
    }
 
    fclose(fp);
  
    if((fp = fopen(filename1, "r")) == NULL)
    {
        printf("Open File %s Failed!\n", filename1);
    }
 
    char temp[100 * LINES];
    fgets(temp, 100 * LINES, fp);
 
    for(i = 0; temp[i] != '\0'; i++)
    {
        if(temp[i] >= 'a' && temp [i] <= 'z')
        {
            temp[i] = temp[i] - 'a' + 'A';
        }
    }
 
    printf("%s\n", temp);
 
    fclose(fp);
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python文件操作练习题是指通过Python代码对文件读取、写入、重命名等操作的练习题目。其中包括创建文件、写入内容、读取文件内容、重命名文件等。 举个例子,假设有以下练习题: 题目:请编写一个Python程序,要求用户输入文件名和字符串,将字符串写入到指定的文件中。 解答:可以使用Python的open函数打开文件,然后使用write方法将字符串写入文件中。可以参考以下代码: ```python filename = input("请输入文件名:") content = input("请输入字符串:") with open(filename, "w") as file: file.write(content) ``` 这段代码中,用户需要输入文件名和字符串,然后使用`open`函数打开文件,指定模式为"w"表示写入模式。然后使用`write`方法将字符串写入文件中。 这是一个简单的Python文件操作练习题的答案,通过这个练习可以增加对Python文件操作的熟悉程度。当然,还有很多其他的练习题目,涉及到文件的读取、重命名、删除等操作,可以根据需要选择不同的练习题目进练习。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python经典基础习题(文件文件夹操作)](https://blog.csdn.net/AQIANKE/article/details/126265497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Python函数文件练习题汇总](https://blog.csdn.net/qq_44034384/article/details/107600201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值