分享

(二).4

 青竹奏乐 2018-04-14
​第 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;  
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多