为了账号安全,请及时绑定邮箱和手机立即绑定

求解关于c++的几个问题,具体内容如下:

求解关于c++的几个问题,具体内容如下:

C++
茅侃侃 2022-05-27 15:11:50
1:编写一个程序,要求定义in为fetream的对象,与输入文件file1.txt建立关联,文件file1.txt的内容如下:abcdefghijklmn定义out为fetream的对象,与输出文件file2.txt建立关联。当文件打开成功后将file1.txt文件的内容转换成大写字母,输出到file2.txt文件中。2:编写一个程序,要求定义in为fetream的对象,与输入文件file.txt建立关联,文件file1.txt的内容如下:XXYYZZ定义out为fetream的对象,与输出文件file2.txt建立关联。当文件打开成功后将file1.txt文件的内容附加到file2.txt文件的尾部。运行前file2.txt文件的内容如下:ABCDEFGHIJKLMN运行后,再查看文件file2.txt的内容。3:编写一个程序,将下面的信息表写入文件stock.txt中:Shen fa zhan 000001shang hai qi che 600104guang ju neng yuan 000096如上,这是一个朋友叫我帮忙完成的东西,但是自己不是学c++的,时间仓促,只能求助各位大侠了,完了后追加分
查看完整描述

2 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

第一个题:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char c;
ifstream in("f:file1.txt",ios::in);
ofstream out("f:file2.txt",ios::out);
while((c=in.get())!=EOF)
{
if(c=='\n'){out.put('\n');continue;}
c=c-32;
out.put(c);
}
in.close();
out.close();
return 0;
}
第二题:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char c;
ifstream in("f:file1.txt",ios::in);
ofstream out("f:file2.txt",ios::app);
out.put('\n');
while((c=in.get())!=EOF)
{
if(c=='\n'){out.put('\n');continue;}
out.put(c);
}
in.close();
out.close();
return 0;
}
第三题:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char c;
ofstream out("f:stock.txt",ios::app);
while((c=getchar())!=EOF)
{
if(c=='\n'){out.put('\n');continue;}
out.put(c);
}
out.close();
return 0;
}

可以融合在一个程序中,由于你的要求限制,我只有给你三个程序了!你自己看下吧,很简单的!!懂了就直接融合,毕竟理解了最重要!



查看完整回答
反对 回复 2022-05-30
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

帮你搞定了
注意运行的时候要先建立 file1.txt

1.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string read;
ifstream in;
ofstream out;
in.open("file1.txt");
out.open("file2.txt");
if(in.fail()||out.fail()){
cout<<"can't open file"<<endl;
system("PAUSE");
exit(1);
}

int num=0;
while(in>>read){
for(int i=0;i<read.length();i++){
if(read[i]>='a'&&read[i]<='z'){
read[i]-=32;
}
}
out<<read<<endl;
}
cout<<"operation done!"<<endl;
in.close();
out.close();
system("PAUSE");
}

2.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string read;
ifstream in;
ofstream out;
in.open("file1.txt");
out.open("file2.txt",ios::app);
if(in.fail()||out.fail()){
cout<<"can't open file"<<endl;
system("PAUSE");
exit(1);
}

int num=0;
while(in>>read){
out<<read<<endl;
}
cout<<"operation done!"<<endl;
in.close();
out.close();
system("PAUSE");
}

3.

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
string read[3]={"Shen fa zhan 000001","shang hai qi che 600104", "guang ju neng yuan 000096"};
ofstream out;
out.open("stock.txt");
if(out.fail()){
cout<<"can't open file"<<endl;
system("PAUSE");
exit(1);
}

for(int i=0;i<3;i++){
out<<read[i]<<endl;
}
cout<<"operation done!"<<endl;
out.close();
system("PAUSE");
}



查看完整回答
反对 回复 2022-05-30
  • 2 回答
  • 0 关注
  • 205 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信