2014-03-19 18 views
0

我試圖寫C++一個rutine讀取如下所示的輸入文件,從最後一列決定例行對輸入文件

1    12  13  0  0  1  0  INLE 
    2    1  12  0  0  1  0  INLE 
    3    11  2  0  0  1  0  INLE 
    4    13  11  0  0  1  0  INLE 
    5    2  8  0  0  2  0  OUTL 
    6    8  9  0  0  2  0  OUTL 
    7    9  10  0  0  2  0  OUTL 
    8    10  3  0  0  2  0  OUTL 
    9    4  5  0  0  3  0  SYMP 
    10    5  6  0  0  3  0  SYMP 
    11    6  7  0  0  3  0  SYMP 
    12    7  1  0  0  3  0  SYMP 
    13    14  4  0  0  4  0  WALL 
    14    16  14  0  0  4  0  WALL 
    15    15  16  0  0  4  0  WALL 
    16    3  15  0  0  4  0  WALL 

在這種情況下,我必須ASIGN的值第二列和第三列與右側最後一列中指定的條件相符。 喜歡的東西,

read the last column; 
    if it reads the word INLE 
    { 
     asign the values of COLUMN2 and COLUMN3 to the pointer &p_InflowNode[i]; 
    } 
if it reads the word OUTL 
    { 
     asign the values of COLUMN2 and COLUMN3 to the pointer &p_NonrefNode[i]; 
    } 
etc... 

所以我的主要問題是,怎樣才能讓我的C++第一次讀的最後一列,然後再決定如何處理第二和第三列的值嗎?

Thaks

回答

2

我建議你這樣做

struct CData 
{ 
    int nC1; 
    int nC2; 
    int nC3; 
    int nC4; 
    int nC5 
    int nC6; 
    int nC7; 
    string sMode; 
    friend ifstream& operator >>(ifstream&, struct& CData); 
} 

你只需要輸入超載運營商,然後 聲明結構和工作對象上:

struct CData cObj; 
ifstream ifIn; 
ifIn.open("yourfile.txt"); 

while(ifIn) 
{ 
    ifIn>>cObj; 
    //now you can use the nested if and else structure here 
} 
0

如果換行一致看在緩衝管線(STD ::函數getline)文件中的行,並檢查結束串我建議。

Check line ending

0

我結束了這樣解決,

#include <fstream> 
#include <string> 
#include <sstream> 
#include <iostream> 
using namespace std; 

int main() 
{ 
    ifstream myfile("ramp.bnd"); 
    string Point1; 
    string Point2; 
    string Boundary; 
    string line,temp; 

    if (myfile.is_open()) 
    { 
     while (getline (myfile,line)) 
     { 
      istringstream iss(line); 
      iss >> temp >> Point1 >> Point2 >> temp >> temp >> temp >> temp >> Boundary; 
      if (Boundary == "INLE") { 
       cout << "Inlet condition!!"; //To add more code 
      } 
     cout << " \n"; 

     } 
     myfile.close(); 
    } 
    else std::cout << "Unable to open file";  
    myfile.close(); 
    return 0; 
} 
相關問題