将有字母A组成的三角形写入文件,然后读出
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main(){
ofstream fout("test.txt",ios::out);
if(!fout){
cout<<"error"<<endl;
return 1;
}
for(int i=0;i<7;i++){
//first space
for(int j=0;j<(6-i);j++)
fout<<" ";
//second 'A'
for(int j=0;j<(2*i+1);j++)
fout<<"A";
fout<<endl;
}
fout.close();
return 0;
#include <fstream>
#include <stdlib.h>
using namespace std;
int main(){
ofstream fout("test.txt",ios::out);
if(!fout){
cout<<"error"<<endl;
return 1;
}
for(int i=0;i<7;i++){
//first space
for(int j=0;j<(6-i);j++)
fout<<" ";
//second 'A'
for(int j=0;j<(2*i+1);j++)
fout<<"A";
fout<<endl;
}
fout.close();
return 0;
}
//读出
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char ch;
ifstream fin("test.txt",ios::in);
if(!fin){
cout<<"error"<<endl;
return 1;
}
while(fin.get(ch)){
cout<<ch;
}
fin.close();
return 0;
}