第 4 题 【要求】编写一个程序,将一个文件中所有小写字母转换成大写字母, 并存储到另一个文件中。 大换小 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream inputFile("test.txt", ios::in); if (inputFile.good()) { string bufffer; string resultStr; inputFile.seekg(ios::beg); while(!inputFile.eof()) { getline(inputFile, bufffer, '\n'); resultStr.append(bufffer); resultStr.append("\r\n"); } inputFile.close(); fstream outputFile("result.txt", ios::app | ios::out); if (outputFile.good()) { outputFile.seekp(ios::beg); _strupr(const_cast<char*>(resultStr.c_str())); outputFile.write(resultStr.c_str(), resultStr.size()); outputFile.close(); } } system("pause"); return 0; } 小换大 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *fp; int i=0; char str[100]; printf("请输入字符串,并且以感叹号!结束:\n"); if((fp=fopen("test.txt","w"))==NULL)//打开输出文件并使fp指向此文件 { printf("无法打开此文件!\n");//如果打开出错就输出打不开的信息 exit(0);//退出程序 } gets(str); while(str[i]!='!') { if(str[i]>='a'&&str[i]<='z') str[i]-=32; fputc(str[i],fp);//向磁盘输出字符,将str所指向的字符,串输出到fp指向的文件中 i++; } fclose(fp); //把转换后的字符串输出 if((fp=fopen("test.txt","r"))==NULL) { printf("File open falied!\n"); exit(0); } printf("转换后的字符串是:\n"); puts(fgets(str,strlen(str)+1,fp));//从文件读取一个字符串,长度是strlen(str),并且显示出来 fclose(fp); return 0; } |
|