C++怎样将读取文本test.txt中的小写字母修改为大写并存入到另一个文本write.txt中?
修改的程序如下,但是修改以后我却无法保存修改后的字符到write.txt。要怎么才可以?#include<iostream>#include<cctype>#includ...
修改的程序如下,但是修改以后我却无法保存修改后的字符到write.txt。要怎么才可以?
#include <iostream>
#include<cctype>
#include<string>
#include <fstream>
using namespace std;
int main()
{
fstream fileread,filewrite;
string upper(string);
string str;
fileread.open("D:\\test.txt",fstream.in);
if(!fileread)
{
cout<<"打开失败"<<endl;
return -1;
}
while (!fileread.eof())
{
str=fileread.get();
cout<<upper(str);
}
filewrite.open("D:\\write.txt",fstream.out);
string line;
while(getline(fileread,line))
{
filewrite<<line<<endl;
}
return 0;
}
string upper(string s)
{
string temp(s);
for(int i=0;i<(int)s.size();i++)
{
temp[i]=toupper(s[i]);
}
return temp;
}
原题还要我将文本中的大写字符改为小写,又是改大写为小写,又是改小写为大写,这样的程序矛盾吗? 展开
#include <iostream>
#include<cctype>
#include<string>
#include <fstream>
using namespace std;
int main()
{
fstream fileread,filewrite;
string upper(string);
string str;
fileread.open("D:\\test.txt",fstream.in);
if(!fileread)
{
cout<<"打开失败"<<endl;
return -1;
}
while (!fileread.eof())
{
str=fileread.get();
cout<<upper(str);
}
filewrite.open("D:\\write.txt",fstream.out);
string line;
while(getline(fileread,line))
{
filewrite<<line<<endl;
}
return 0;
}
string upper(string s)
{
string temp(s);
for(int i=0;i<(int)s.size();i++)
{
temp[i]=toupper(s[i]);
}
return temp;
}
原题还要我将文本中的大写字符改为小写,又是改大写为小写,又是改小写为大写,这样的程序矛盾吗? 展开
2个回答
展开全部
原题要求你把test.txt中大写字母转换成小写,小写转换成大写 然后存进write.txt。
就是大小写互换 存入另一个文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fileread,filewrite;
char ch;
fileread.open("test.txt",fstream.in);
filewrite.open("write.txt",fstream.out);
if(!fileread)
{
cout<<"打开失败"<<endl;
return -1;
}
while (!fileread.eof())
{
ch = fileread.get();
if(ch>='a'&&ch<='z')
ch -= 32;
else if(ch>='A'&&ch<='Z')
ch += 32;
filewrite.put(ch);
}
fileread.close();
filewrite.close();
return 0;
}
就是大小写互换 存入另一个文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fileread,filewrite;
char ch;
fileread.open("test.txt",fstream.in);
filewrite.open("write.txt",fstream.out);
if(!fileread)
{
cout<<"打开失败"<<endl;
return -1;
}
while (!fileread.eof())
{
ch = fileread.get();
if(ch>='a'&&ch<='z')
ch -= 32;
else if(ch>='A'&&ch<='Z')
ch += 32;
filewrite.put(ch);
}
fileread.close();
filewrite.close();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不矛盾
#include<stdio.h>
int main()
{
freopen("D:\\test.txt","r",stdin);
freopen("D:\\write.txt","w",stdout);
char s[1005];
int i;
while(gets(s))
{
for(i=0;s[i];i++)
if(s[i]>='a'&&s[i]<='z')
putchar(s[i]-32);
else if(s[i]>='A'&&s[i]<='Z')
putchar(s[i]+32);
else
putchar(s[i]);
puts("");
}
}
#include<stdio.h>
int main()
{
freopen("D:\\test.txt","r",stdin);
freopen("D:\\write.txt","w",stdout);
char s[1005];
int i;
while(gets(s))
{
for(i=0;s[i];i++)
if(s[i]>='a'&&s[i]<='z')
putchar(s[i]-32);
else if(s[i]>='A'&&s[i]<='Z')
putchar(s[i]+32);
else
putchar(s[i]);
puts("");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询