C语言:将文件alpha.txt中的小写字母转换为大写字母,再将转换后的大写字母追加到该文件中,咋写?

如题所述

设文件alpha.txt在当前目录下——

(副本是原文件,下面是处理后的文件)

代码文本:

#include "stdio.h"

int main(int argc,char *argv[]){

FILE *fp,*fpt;

char ch;

fp=fopen("alpha.txt","r+");

if(!fp || !(fpt=fopen("tfr.txt","w"))){

printf("Open the file failed, exit...\n");

return 0;

}

while((ch=fgetc(fp))!=EOF){

if(ch>='a' && ch<='z'){

fseek(fp,-1L,SEEK_CUR);

fputc(ch-=32,fp);

fputc(ch,fpt);

fseek(fp,0L,SEEK_CUR);

}

}

fclose(fp);

fclose(fpt);

fp=fopen("alpha.txt","a");

if(!fp || !(fpt=fopen("tfr.txt","r"))){

printf("Open the file failed, exit...\n");

return 0;

}

while((ch=fgetc(fpt))!=EOF)

fputc(ch,fp);

fclose(fp);

fclose(fpt);

remove("tfr.txt");

return 0;

}

供参考……

温馨提示:答案为网友推荐,仅供参考
相似回答