Thread: Reading file conditionally

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    Reading file conditionally

    Hi, I am trying to write a code to read a file
    I want to read it conditionally.
    for example, if the first few line of file are

    (0 " Created by : Fluent_V6 Interface Vers. 11.6.2")
    (2 3)
    (0 "Node Section")
    (10 (0 1 24f 0 3))
    (10 (2 1 24f 1 3)

    If I read (0 at beginning, then I want to ignore it.
    If I read (2 at the beginning, I want do some action on it and so on

    Can someone help ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Can someone help ?
    How To Ask Questions The Smart Way

    How about showing us what you CAN do at the moment.
    Say open the file and read all the lines.

    If you can't open and read the files, there isn't a lot of point explaining the finer points of parsing, if you can't manage the basics.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    I can use 'getline' till end of the file.
    I was trying to use,

    Code:
           
    ifstream inmesh;
    inmesh.open(Filename);
    while(!(inmesh.eof())
    string line;
    getline(inmesh,line);
    if(line.find("(0 ")==0)
    {
    do something......
    }
    something of this sort.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You can even do

    while ( getline(inmesh,line) )
    and save yourself from eof() troubles (see the FAQ)

    > I was trying to use,
    ...
    > if(line.find("(0 ")==0)
    So how did it go?
    So far, it doesn't seem entirely unreasonable.

    How complicated is the input file anyway - post a link to some online specs for the file format.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    ok my file goes like this

    (0 " Created by : Fluent_V6 Interface Vers. 11.6.2")
    (2 3)
    (0 "Node Section")
    (10 (0 1 24f 0 3))
    (10 (2 1 24f 1 3)
    (
    6.029458523 0.7618522644 1
    9.07050705 0.2473477572 1
    0 0.3249595165 0.3750339746
    2.533670902 0.2626713812 1
    ....so on for 24f (hexadecimal) lines
    so what it saying is after (10 (2 1 24f 1 3) there are 24f lines of numbers like shown here. So I want to read like,
    if you encounter (10 at start of line then read this 24f as say 'n' and then read numbers in these 24f lines and store these numbers in array.

    after 24f lines again some similar section with different number index i.e. instead of (10 there would be (13 which basically says that I need to store the numbers below it in different array.

    The problem I am having is that I am not sure if(line.find==0) is working for me or is it a right way or not. because if say it encounters the characters I am hoping for at the start of line i.e. (10 , I need to read the lines following it and store it. But I dont think getline command is working in that case.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I think you should try learning C++ for a while, until you understand the basics.

    The problem I am having is that I am not sure if(line.find==0) is working for me or is it a right way or not. because if say it encounters the characters I am hoping for at the start of line i.e. (10 , I need to read the lines following it and store it. But I dont think getline command is working in that case.
    Because at the moment, you look like someone with a problem to solve and an empty toolbox.

    Try this
    Code:
    int lineNum = 1;
    while ( getline(inmesh,line) ) {
      cout lineNum << " " << line << endl;
      lineNum++;
    }
    Does it print the WHOLE file?
    If it does, then getline is working.

    Now try this
    Code:
    int lineNum = 1;
    while ( getline(inmesh,line) ) {
      cout << line << endl;
      if(line.find("(0 ")==0) {
        cout lineNum << " " << line << endl;
      }
      lineNum++;
    }
    Is it finding all the lines?

    Is it finding lines which don't interest you at this stage?
    Well that's where you come in, work out a better test so it finds ONLY the lines which interest you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to conditionally branch this?
    By roll cast in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2011, 01:22 PM
  2. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  3. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  4. Replies: 3
    Last Post: 02-15-2008, 10:54 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM