CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2010
    Posts
    34

    Reading Unstructure Text File using CFile in MFC

    Hi,

    I wondering if anybody can help me. I intend to use CFile to trap interested data for a unstructured text file. Unstructured text file, meaning data is not sorted. Following is the part of the text file:

    Code:
    ============================================================================================================================
         OFFPIPE - OFFSHORE PIPELINE ANALYSIS SYSTEM - VERSION NO: 2.07 GJ       DATE -  8/ 4/2010    TIME - 12:48:40     PAGE   37
         PROJECT - PCSB 6IN BEA TO BEB SIDEWALK - LIFTING VIA DLB264                               JOB NO. - 50044
         USER ID - I.HAMID                                           LICENSED USER: GLOBAL OFFSHORE MALAYSIA SDN BHD      CASE    1
        ============================================================================================================================
    
                           S T A T I C   P I P E   C O O R D I N A T E S,   F O R C E S   A N D   S T R E S S E S
    
     ===================================================================================================================================
     NODE    PIPE           X         Y         Z      HORIZ    VERT      PIPE     TENSILE    HOOP    BENDING STRESSES    TOTAL   PERCNT
      NO.   SECTION       COORD     COORD     COORD    ANGLE    ANGLE    LENGTH     STRESS   STRESS     VERT     HORIZ   STRESS    YIELD
                         (FEET)    (FEET)    (FEET)   (DEG )   (DEG )    (FEET)     (KSI )   (KSI )    (KSI )   (KSI )   (KSI )   (PCT )
     ===================================================================================================================================
        1   SEABED        30.00   -242.92      0.00    0.000   -0.035     0.000      -0.11     0.00      0.00     0.00     0.11     0.17
        2   SEABED        15.00   -242.90      0.00    0.000   -0.060    15.000      -0.11     0.00      0.44     0.00     0.55     0.84
    
        3   LAYBARGE       0.00   -242.89      0.00    0.000    0.006    30.000      -0.11     0.00     -3.05     0.00     3.16     4.86
    
        5   SEABED       -25.00   -242.91      0.00    0.000    0.010    55.000      -0.11     0.00      0.39     0.00     0.50     0.77
        6   SEABED       -50.00   -242.91      0.00    0.000   -0.002    80.000      -0.11     0.00     -0.03     0.00     0.14     0.21
        7   SEABED       -75.00   -242.91      0.00    0.000    0.000   105.000      -0.11     0.00     -0.03     0.00     0.14     0.21
        9   SEABED      -100.00   -242.91      0.00    0.000    0.000   130.000      -0.11     0.00      0.00     0.00     0.11     0.17
       10   SEABED      -125.00   -242.91      0.00    0.000    0.000   155.000      -0.11     0.00      0.00     0.00     0.11     0.17
       11   SEABED      -150.00   -242.91      0.00    0.000    0.000   180.000      -0.11     0.00     -0.03     0.00     0.14     0.21
       13   SEABED      -175.00   -242.91      0.00    0.000    0.000   205.000      -0.11     0.00      0.00     0.00     0.11     0.17
       14   SEABED      -200.00   -242.91      0.00    0.000    0.000   230.000      -0.11     0.00      0.00     0.00     0.11     0.17
       15   SEABED      -225.00   -242.91      0.00    0.000    0.000   255.000      -0.11     0.00     -0.03     0.00     0.14     0.21
       17   SEABED      -245.00   -242.91      0.00    0.000    0.000   275.000      -0.11     0.00      0.01     0.00     0.11     0.17
       18   SEABED      -265.00   -242.91      0.00    0.000    0.000   295.000      -0.11     0.00      0.00     0.00     0.11     0.17
       19   SEABED      -285.00   -242.91      0.00    0.000    0.000   315.000      -0.11     0.00      0.00     0.00     0.11     0.17
       20   SEABED      -305.00   -242.91      0.00    0.000    0.000   335.000      -0.11     0.00      0.00     0.00     0.11     0.17
       21   SEABED      -325.00   -242.91      0.00    0.000    0.000   355.000      -0.11     0.00      0.00     0.00     0.11     0.17
       22   SEABED      -345.00   -242.91      0.00    0.000    0.000   375.000      -0.11     0.00      0.00     0.00     0.11     0.17
       23   SEABED      -365.00   -242.91      0.00    0.000    0.000   395.000      -0.11     0.00      0.00     0.00     0.11     0.17
       24   SEABED      -385.00   -242.91      0.00    0.000    0.000   415.000      -0.11     0.00      0.00     0.00     0.11     0.17
       25   SEABED      -405.00   -242.91      0.00    0.000    0.000   435.000      -0.11     0.00      0.00     0.00     0.11     0.17
       26   SEABED      -425.00   -242.91      0.00    0.000    0.000   455.000      -0.11     0.00      0.00     0.00     0.11     0.17
       27   SEABED      -445.00   -242.91      0.00    0.000    0.000   475.000      -0.11     0.00      0.00     0.00     0.11     0.17
       28   SEABED      -465.00   -242.91      0.00    0.000    0.000   495.000      -0.11     0.00      0.00     0.00     0.11     0.17
       29   SEABED      -485.00   -242.91      0.00    0.000    0.000   515.000      -0.11     0.00      0.00     0.00     0.11     0.17
       30   SEABED      -505.00   -242.91      0.00    0.000    0.000   535.000      -0.11     0.00      0.00     0.00     0.11     0.17
       31   SEABED      -525.00   -242.91      0.00    0.000    0.000   555.000      -0.11     0.00      0.00     0.00     0.11     0.17
       32   SEABED      -545.00   -242.91      0.00    0.000    0.000   575.000      -0.11     0.00      0.00     0.00     0.11     0.17


    As you can see, there are some data in numeric and some in text. Where, in the middle is just empty spaces, not tabs. I need to separate it, store it in vector of int and CString.

    The problem is, I got no idea how to do the reading, and storing in variable (with respect of numeric or CSTring data).

    Can anybody give me some idea? Thanks for reading this

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Reading Unstructure Text File using CFile in MFC

    First off, this text file is not really unstructured. It is not even unsorted: It is properly sorted by the NODE NO. field.

    The CFile class isn't really suitible for data like this. It dosn't even have a method to read in individual lines of a text file. In fact, even MS's own documentation on CFile recommends to use iostream classes for data like that.

    Are there really compelling reasons for you to use CFile? If not, I think you should reconsider your choice.

    HTH

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Reading Unstructure Text File using CFile in MFC

    1) I would use ifstream , since it can read both string and numeric
    data in a typesafe manner.

    2) Create a struct to hold each row of data.

    Code:
    struct Data
    {
        int node;
        std::string pipe_section;
        float x;
        float y;
        float z;
        float horiz_angle;
        float vert_angle;
        float pipe_length;
        float tensile_stress;
        float hoop_stress;
        float bending_vert;
        float stresses_horiz;
        float total_stress;
        float percent_yield;
    };
    3) overload operator >> for the Data ...
    Code:
    std::istream & operator >> (std::istream & in , Data & d)
    {
        in >> d.node           >> d.pipe_section 
           >> d.x              >> d.y            >> d.z
           >> d.horiz_angle    >> d.vert_angle   >> d.pipe_length  
           >> d.tensile_stress >> d.hoop_stress  >> d.bending_vert 
           >> d.stresses_horiz >> d.total_stress >> d.percent_yield;
    
        return in;
    }
    4) The hard part is skipping the first part of the file ...
    Code:
        ifstream in("your file name");
    
        string field,line;
    
        while (field!="NO." && in >> field)
        {
            getline(in,line);
        }
        getline(in,line);
        getline(in,line);
    5) read into a vector ...

    Code:
        vector<Data> v;
    
        Data d;
    
        while (in >> d)
        {
            v.push_back(d);
            cout << d.node << " " << d.percent_yield << "\n";
        }

  4. #4
    Join Date
    Jun 2010
    Posts
    34

    Re: Reading Unstructure Text File using CFile in MFC

    Thanks Guys.

    Actually there a alot more of that file that I didn't paste into this post. Alot f rubbish data and the column is not always arrange in that sequence plus sometime its include new column for new data type.

    So I guess, I better use ifstream

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Reading Unstructure Text File using CFile in MFC

    Philip's suggestion seems the best for this (relatively) simple case. If you encounter more complex structures to parse, you could use the boost's spirit library (http://boost-spirit.com/home/doc/).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Reading Unstructure Text File using CFile in MFC

    Quote Originally Posted by Eri523 View Post
    First off, this text file is not really unstructured. It is not even unsorted: It is properly sorted by the NODE NO. field.

    The CFile class isn't really suitible for data like this. It dosn't even have a method to read in individual lines of a text file. In fact, even MS's own documentation on CFile recommends to use iostream classes for data like that.

    Are there really compelling reasons for you to use CFile? If not, I think you should reconsider your choice.

    HTH
    But there is a class derived from it, CStdioFile, that is specifically for text files.

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Reading Unstructure Text File using CFile in MFC

    Quote Originally Posted by GCDEF View Post
    But there is a class derived from it, CStdioFile, that is specifically for text files.
    Ah, interesting. I didn't encounter that yet. Thank you for the pointer. Do you happen to know when it was introduced?

    But this one doesn't have parsing mechanisms either. So Philip's iostream solution still appears to be the best choice up to now for the OP.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Reading Unstructure Text File using CFile in MFC

    Quote Originally Posted by Eri523 View Post
    Ah, interesting. I didn't encounter that yet. Thank you for the pointer. Do you happen to know when it was introduced?

    But this one doesn't have parsing mechanisms either. So Philip's iostream solution still appears to be the best choice up to now for the OP.
    Don't know but I know it goes way, way back to early MFC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured