输入一个字符串,将小写字母转换成大写字母,然后输出到一个文件“test”中保存。输入的字符串以!结束


代码如下:

				
#include<stdio.h>
#include<stdlib.h>
#define SIZE 500
int main()
{
     FILE *fp;
     char a[SIZE],ch;
     int i=0,sum;
     printf("Please input a string:\n");
     gets(a);//输入字符串
     while(a[i]!='!')//将小写字母转化为大写字母
     {
         if(a[i]>='a'&&a[i]<='z')
             a[i]=a[i]-32;
         i++;
     }
     sum=i;
     if((fp=fopen("test","w"))==NULL)
     {
         printf("cannot open file\n");
         exit(0);
     }
     printf("The sorted string is:\n");
     for(i=0;i<sum;i++)//将整理后的字符串写入文件test
     {
         fputc(a[i],fp);
         putchar(a[i]);
     }
     fclose(fp);
     printf("\n");
     if((fp=fopen("test","r"))==NULL)
     {
         printf("cannot open file\n");
         exit(0);
     }
     printf("file test contents are:\n");
     ch=fgetc(fp);
     while(ch!=EOF)//查看文件test的内容并在屏幕上显示
     {
         putchar(ch);
         ch=fgetc(fp);
     }
     fclose(fp);
     printf("\n");
     return 0;
}




联系我们 | 友情链接