-
April 13th, 2010, 08:50 AM
#1
general solution to reading fixed format files?
one approach to reading fixed format data in files is to
pre-define a class or struct of data types that map to columns in a file
record and write custom code to read the data fields as string data, convert the strings to the appropriate data types, and populate the members of the struct.
this is extremely limiting since one essentially must code compile a file specific "read" function for every file.
most general software permits the definition of of column bounds and datatype n the form of an "input format" to read the data. i presume this is possible in c++ but can not figure it out in any relativley simple manner.
for example, is it possible to dynamically define and create a struct or class to hold record fields from user supplied info on the column bounds and data type? or would one recommend that one sets up a generic class of record consisting of, say, pointers to certain data types and then programatically
determine the number of integer fields, string fields, etc from the user input and then allocate arrays of data types ....
appreciate any insight.
thx
-
April 13th, 2010, 09:04 AM
#2
Re: general solution to reading fixed format files?
Well, the first question is: do you have control over the file format, or are you constrained to read what's there?
If you have control over it, then you could just use something like Boost.Serialization to write the file, which makes reading it back in easy.
-
April 13th, 2010, 09:25 AM
#3
Re: general solution to reading fixed format files?
no - no control whatsoever - think of it as a fixed format file just appears along with field defintions: the column bounds and datatype. example of
possible record defs:
format:
1 - 4 year int
10-12 mon string (e.g. "jan" "feb" ...)
20-26 AvgTemp double
file:
1 2 3 4
....5....0....5....0....5....0....5....0
1984 jan 87.111
1987 mar 87.111
-
April 25th, 2010, 11:55 AM
#4
Re: general solution to reading fixed format files?
think this really distill to a "dynamic class definition" or "dynamic struct definition". google searches return a lot of links but nothing useful. any comments/ideas?
the basic idea would be that the code would be told that tha a record in a file
consiste of some number of different data types and a struct, say, would be dynamically defined to hold the data.
thx in advance.
-
April 25th, 2010, 12:14 PM
#5
Re: general solution to reading fixed format files?
Just throwing an idea here: you could have a class that represents a record, and classes that represent the field types. A record would then consist of vectors of each of the field types. Based on some format string, you can then read into a record, and insert into the vectors accordingly.
-
April 25th, 2010, 04:55 PM
#6
Re: general solution to reading fixed format files?
I suppose you could use a std::map<std::string, boost::any>. Of course, you'd need to store the data types somewhere to get the results back out of the any......how best to do that really depends on the larger context.
If you know you'll have a limited number of possible types----say, double, int and string are the only possibilities----then you could use a boost::variant instead, which partially solves that problem. Beware, boost::variants produce some of the longest compile errors I have ever seen when you get them wrong!
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
|