#include <iostream> #include <fstream> #include <cstdlib> //exit() #include <iomanip> #include <cmath> #include <string> #include <ctime> using namespace std; struct NODE { int node; double Fx,Fz,Fy; }; struct LOAD { string LC; NODE L[5]; }L1[50]; int main() { char fname[60]; int i,j; int CC; ifstream fin; cout << "Please Enter NSALOAD File:"; cin.getline(fname,60); fin.open(fname); if(!fin.is_open()) { cout << "Fail to open file!" << endl; cin.get(); exit(EXIT_FAILURE); } fin >> CC; for(i=0;i<CC;i++) { getline(fin,L1[i].LC); for(j=0;j<5;j++) { fin >> L1[i].L[j].node; fin >> L1[i].L[j].Fx; fin >> L1[i].L[j].Fz; fin >> L1[i].L[j].Fy; } } fin.close(); ofstream fout; fout.open("NSALOAD.TXT"); for(i=0;i<CC;i++) { fout << L1[i].LC << endl; for(j=0;j<5;j++) { fout << right; fout << fixed; fout << setw(5) << L1[i].L[j].node; fout << setw(12) << setprecision(3) << L1[i].L[j].Fx; fout << setw(12) << setprecision(3) << L1[i].L[j].Fz; fout << setw(12) << setprecision(3) << L1[i].L[j].Fy << endl; } } fout << endl; for(i=0;i<CC;i++) { fout << "LOAD " << i+8 << " LOADTYPE None " << L1[i].LC << endl; fout << "JOINT LOAD" << endl; for(j=0;j<5;j++) { fout << L1[i].L[j].node << " FX " << L1[i].L[j].Fx << " FY " << (-1)*L1[i].L[j].Fy << " FZ " << L1[i].L[j].Fz << endl; } } fout.close(); return 0; }
下面是NSA.TXT文件内容:
21
1 90 MAXWIND WIRE
97 5.570 0.000 5.820
95 5.570 0.000 5.820
99 6.970 0.000 9.820
103 6.970 0.000 9.820
101 6.970 0.000 9.820
2 0 MAXWIND WIRE
97 0.000 1.390 5.820
95 0.000 1.390 5.820
99 0.000 1.740 9.820
103 0.000 1.740 9.820
101 0.000 1.740 9.820
3 90 MAXWIND WIRE GMIN
97 5.570 0.000 1.230
95 5.570 0.000 1.230
99 6.970 0.000 2.330
103 6.970 0.000 2.330
101 6.970 0.000 2.330
4 0 MAXWIND WIRE GMIN
97 0.000 1.390 1.230
95 0.000 1.390 1.230
99 0.000 1.740 2.330
103 0.000 1.740 2.330
101 0.000 1.740 2.330
5 ICE WIRE
97 2.100 0.000 12.500
95 2.100 0.000 12.500
99 1.530 0.000 13.450
103 1.530 0.000 13.450
101 1.530 0.000 13.450
6 BROKEN LEFT EARTH
97 0.000 0.000 12.170
95 0.000 39.770 12.170
99 0.000 0.000 13.250
103 0.000 0.000 13.250
101 0.000 0.000 13.250
7 BROKEN RIGHT EARTH
97 0.000 39.770 12.170
95 0.000 0.000 12.170
99 0.000 0.000 13.250
103 0.000 0.000 13.250
101 0.000 0.000 13.250
8 BROKEN MIDDLE WIRE
97 0.000 0.000 12.170
95 0.000 0.000 12.170
99 0.000 24.820 13.250
103 0.000 0.000 13.250
101 0.000 0.000 13.250
9 BROKEN LEFT WIRE
97 0.000 0.000 12.170
95 0.000 0.000 12.170
99 0.000 0.000 13.250
103 0.000 0.000 13.250
101 0.000 24.820 13.250
10BROKEN RIGHT WIRE
97 0.000 0.000 12.170
95 0.000 0.000 12.170
99 0.000 0.000 13.250
103 0.000 24.820 13.250
101 0.000 0.000 13.250
11LIFT RIGHT EARTH
97 0.700 0.000 15.320
95 0.000 0.000 0.000
99 0.000 0.000 0.000
103 0.000 0.000 0.000
101 0.000 0.000 0.000
12LIFT LEFT EARTH
97 0.700 0.000 5.820
95 0.700 0.000 15.320
99 0.000 0.000 0.000
103 0.000 0.000 0.000
101 0.000 0.000 0.000
13LIFT LEFT WIRE
97 0.700 0.000 5.820
95 0.700 0.000 5.820
99 0.850 0.000 9.820
103 0.850 0.000 26.020
101 0.000 0.000 0.000
14LIFT MIDDLE WIRE
97 0.700 0.000 5.820
95 0.700 0.000 5.820
99 0.850 0.000 26.020
103 0.000 0.000 0.000
101 0.000 0.000 0.000
15LIFT RIGHT WIRE
97 0.700 0.000 5.820
95 0.700 0.000 5.820
99 0.850 0.000 9.820
103 0.850 0.000 9.820
101 0.850 0.000 26.020
16ANCHOR RIGHT EARTH
97 0.700 1.750 16.500
95 0.000 0.000 0.000
99 0.000 0.000 0.000
103 0.000 0.000 0.000
101 0.000 0.000 0.000
17ANCHOR LEFT EARTH
97 0.700 1.590 13.080
95 0.700 1.750 16.500
99 0.000 0.000 0.000
103 0.000 0.000 0.000
101 0.000 0.000 0.000
18ANCHOR LEFT WIRE
97 0.700 1.590 13.080
95 0.700 1.590 13.080
99 0.850 2.490 21.020
103 0.850 2.740 26.830
101 0.000 0.000 0.000
19ANCHOR MIDDLE WIRE
97 0.700 1.590 13.080
95 0.700 1.590 13.080
99 0.850 2.740 26.830
103 0.000 0.000 0.000
101 0.000 0.000 0.000
20ANCHOR RIGHT WIRE
97 0.700 1.590 13.080
95 0.700 1.590 13.080
99 0.850 2.490 21.020
103 0.850 2.490 21.020
101 0.850 2.740 26.830
21DEFLECTION
97 0.253 0.000 5.820
95 0.253 0.000 5.820
99 0.317 0.000 9.820
103 0.317 0.000 9.820
101 0.317 0.000 9.820
40
第一行你只读入了21,遗留的回车符被之后的getline读走了,导致之后的读取都错误
PS:每读完5行也会有遗留的回车,也需要清掉
#include <iostream> #include <fstream> #include <cstdlib> //exit() #include <iomanip> #include <cmath> #include <string> #include <ctime> using namespace std; struct NODE { int node; double Fx,Fz,Fy; }; struct LOAD { string LC; NODE L[5]; }L1[50]; int main() { char fname[60]; int i,j; int CC; ifstream fin; cout << "Please Enter NSALOAD File:"; cin.getline(fname,60); fin.open(fname); if(!fin.is_open()) { cout << "Fail to open file!" << endl; cin.get(); exit(EXIT_FAILURE); } fin >> CC; string buffer; getline(fin,buffer); for(i=0;i<CC;i++) { getline(fin,L1[i].LC); for(j=0;j<5;j++) { fin >> L1[i].L[j].node; fin >> L1[i].L[j].Fx; fin >> L1[i].L[j].Fz; fin >> L1[i].L[j].Fy; } getline(fin,buffer); } fin.close(); ofstream fout; fout.open("NSALOAD.TXT"); for(i=0;i<CC;i++) { fout << L1[i].LC << endl; for(j=0;j<5;j++) { fout << right; fout << fixed; fout << setw(5) << L1[i].L[j].node; fout << setw(12) << setprecision(3) << L1[i].L[j].Fx; fout << setw(12) << setprecision(3) << L1[i].L[j].Fz; fout << setw(12) << setprecision(3) << L1[i].L[j].Fy << endl; } } fout << endl; for(i=0;i<CC;i++) { fout << "LOAD " << i+8 << " LOADTYPE None " << L1[i].LC << endl; fout << "JOINT LOAD" << endl; for(j=0;j<5;j++) { fout << L1[i].L[j].node << " FX " << L1[i].L[j].Fx << " FY " << (-1)*L1[i].L[j].Fy << " FZ " << L1[i].L[j].Fz << endl; } } fout.close(); return 0; }