level 4
冰潇汀
楼主
请问,我最近写一个小程序,就是从某数据文件里读文件,然后按另一个格式写到另一个文件里,比较简单,但是我是自学的,很多东西都不太懂,比如,我想用函数,main函数与我自己写的函数都要打开那个文件往文件里写东西,可是在我自己写的函数里,如果再open一个那个函数,貌似就把main函数里写的东西给抹掉了,就是两个函数同时打开一个文件,往里面写东西,这一般怎么操作呢?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
const int SIZE = 60;
const int CODE = 20;
void coninfo();
int code[CODE];
int main()
{
using namespace std;
ifstream fin;
ofstream fout;
char filename[SIZE];
cout << "Please Enter a TTA File Name:_______\b\b\b\b\b\b\b";
cin.getline(filename,SIZE);
cin.get();
fin.open(filename);
if (!fin.is_open())
{
cout << "Could not open the file " << filename << " ." << endl;
cout << "Program Terminating." << endl;
exit(EXIT_FAILURE);
}
string kv;
string proname;
string engname;
getline(fin,kv);
while(fin.good())
{
getline(fin,proname);
getline(fin,engname);
int i;
for(i=0;i<CODE;i++)
{
fin >> code[i];
}
break;
}
fout.open("xxx.txt");
fout << "Voltage: " << kv << endl;
fout << "Project: " << proname << endl;
fout << "Engineer: " << engname << endl << endl;
coninfo();
cin.get();
return 0;
}
void coninfo()
{
using namespace std;
ofstream fout;
cout << "Regulation Code : " << right << setw(10) << code[0] << endl;
cout << "Lines_1st Kind Node : " << right << setw(6) << code[1] << endl;
cout << "Lines_2nd Kind Node : " << right << setw(6) << code[2] << endl;
cout << "Lines_Main Member : " << right << setw(8) << code[3] << endl;
cout << "Lines_Redu Member : " << right << setw(8) << code[4] << endl;
cout << "Height & Leg Number : " << right << setw(6) << code[5] << endl;
cout << "Load Case Summary : " << right << setw(8) << code[6] << endl;
cout << "Wire Connetion Node : " << right << setw(5) << code[7] << endl;
cout << "Wind Load act Node : " << right << setw(6) << code[8] << endl;
}
2014年12月14日 15点12分
1
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
const int SIZE = 60;
const int CODE = 20;
void coninfo();
int code[CODE];
int main()
{
using namespace std;
ifstream fin;
ofstream fout;
char filename[SIZE];
cout << "Please Enter a TTA File Name:_______\b\b\b\b\b\b\b";
cin.getline(filename,SIZE);
cin.get();
fin.open(filename);
if (!fin.is_open())
{
cout << "Could not open the file " << filename << " ." << endl;
cout << "Program Terminating." << endl;
exit(EXIT_FAILURE);
}
string kv;
string proname;
string engname;
getline(fin,kv);
while(fin.good())
{
getline(fin,proname);
getline(fin,engname);
int i;
for(i=0;i<CODE;i++)
{
fin >> code[i];
}
break;
}
fout.open("xxx.txt");
fout << "Voltage: " << kv << endl;
fout << "Project: " << proname << endl;
fout << "Engineer: " << engname << endl << endl;
coninfo();
cin.get();
return 0;
}
void coninfo()
{
using namespace std;
ofstream fout;
cout << "Regulation Code : " << right << setw(10) << code[0] << endl;
cout << "Lines_1st Kind Node : " << right << setw(6) << code[1] << endl;
cout << "Lines_2nd Kind Node : " << right << setw(6) << code[2] << endl;
cout << "Lines_Main Member : " << right << setw(8) << code[3] << endl;
cout << "Lines_Redu Member : " << right << setw(8) << code[4] << endl;
cout << "Height & Leg Number : " << right << setw(6) << code[5] << endl;
cout << "Load Case Summary : " << right << setw(8) << code[6] << endl;
cout << "Wire Connetion Node : " << right << setw(5) << code[7] << endl;
cout << "Wind Load act Node : " << right << setw(6) << code[8] << endl;
}