-
August 9th, 2010, 07:17 PM
#1
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
-
August 9th, 2010, 08:24 PM
#2
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
-
August 9th, 2010, 09:12 PM
#3
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";
}
-
August 9th, 2010, 09:25 PM
#4
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
-
August 10th, 2010, 02:41 AM
#5
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/).
-
August 10th, 2010, 07:17 AM
#6
Re: Reading Unstructure Text File using CFile in MFC
Originally Posted by Eri523
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.
-
August 10th, 2010, 09:01 AM
#7
Re: Reading Unstructure Text File using CFile in MFC
Originally Posted by GCDEF
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.
-
August 10th, 2010, 09:18 AM
#8
Re: Reading Unstructure Text File using CFile in MFC
Originally Posted by Eri523
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|