0

I am trying to write a rutine for C++ that reads an input file like the following,

   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

In this case, I must asign the values of the second and third column to the condition specified on the last column of the right. Something like,

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...

So my main question is, how can I make C++ to read first the last column, and then decide what to do with the values of the second and third columns?

Thaks

heri-salmas
  • 103
  • 2
  • 12

3 Answers3

2

I suggest you do this

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

You just need to overload the input operator and then declare an object of the struct and work on it:

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

while(ifIn)
{
   ifIn>>cObj;
   //now you can use the nested if and else structure here
}
AndyG
  • 39,700
  • 8
  • 109
  • 143
balaji
  • 81
  • 6
0

I would suggest if line breaks are consistent to read the file line by line (std::getLine) in a buffer, and check the ending string.

Check line ending

Community
  • 1
  • 1
j-p
  • 1,622
  • 10
  • 18
0

I ended up solving it like this,

#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;
}
heri-salmas
  • 103
  • 2
  • 12