1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#define die(msg) {cerr << msg << endl; exit(1);}
using namespace std;
struct Eip
{
string col1;
string col2;
string col3;
string col4;
};
int showSize(fstream &);
void inputData(fstream &, Eip [], int);
void outputData(fstream &, Eip [], int);
int main()
{
fstream fs("Book1.txt", ios::in);
fstream fsout("test.txt", ios::out);
if(!fs.is_open()) die("Can not open Book1.txt!");
if(!fsout.is_open()) die("Can not open test.txt!");
int size = showSize(fs);
Eip *name;
name = new Eip[size];
inputData(fs, name, size);
outputData(fsout, name, size);
fs.close();
fsout.close();
return(0);
}
int showSize(fstream &fs)
{
string line;
int count = 0;
while(getline(fs,line)) {
count++;
}
return count;
}
void inputData(fstream &fs, Eip name[], int size)
{
for(int i = 0; i < size; i++) {
getline(fs, name[i].col1, '\t');
getline(fs, name[i].col2, '\t');
getline(fs, name[i].col3, '\t');
getline(fs, name[i].col4, '\n');
}
}
void outputData(fstream &fsout, Eip name[], int size)
{
for(int i = 1; i < size; i++) {
fsout << setw(4) << setfill('0');
fsout << name[i].col1;
if(name[i].col2 == "")
fsout << " ";
else
fsout << name[i].col2;
fsout << setw(3) << setfill('0');
fsout << name[i].col3;
if(name[i].col4 == "")
fsout << " " << endl;
else
fsout << name[i].col4 << " " << endl;
}
}
|