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;
}
可以融合在一个程序中,由于你的要求限制,我只有给你三个程序了!你自己看下吧,很简单的!!懂了就直接融合,毕竟理解了最重要!
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");
}
- 2 回答
- 0 关注
- 256 浏览
添加回答
举报