C语言程序设计 练习题参考答案 第八章 文件(1)

/* 8.5 从键盘输入一行字符,将其中小写字母转换为大写字母 */
#include "stdio.h"
void main()
{
    FILE *fp;
    char ch;
    if((fp=fopen("c:\\ex85.txt","w"))==NULL)
      {
       printf("不能创建文件c:\\ex85.txt");
       exit(1);
      }
    printf("请输入一行字符\n");
    while((ch=getchar())!='\n')
      {
        if(ch>='a' && ch<='z')
           ch=ch-32;
        fputc(ch,fp);
      }
    fclose(fp);
    printf("操作成功");
}
 /* 8.7  把一个ASCII文件连接在另外一个ASCII文件之后。 把c:\\ex87_1.txt中的字符连接在c:\\ex87_2.txt中的之后*/
#include "stdio.h"
void main()
{
    FILE *fp1,*fp2;
    char ch;
    if((fp1=fopen("c:\\ex87_1.txt","r"))==NULL)
      {
       printf("不能打开文件c:\\ex87_1.txt");
       exit(1);
      }
    if((fp2=fopen("c:\\ex87_2.txt","a"))==NULL)
      {
       printf("不能打开文件c:\\ex87_2.txt");
       exit(1);
      }
    while((ch=fgetc(fp1))!=EOF)
      {
        fputc(ch,fp2);
      }
    fclose(fp1);
    fclose(fp2);
    printf("操作成功,请打开c:\\ex87_2.txt查看结果");
}
posted @ 2008-05-18 20:20  emanlee  阅读(1599)  评论(0编辑  收藏  举报