Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I've been working on this project for quite a while, and have asked questions and got solutions too. But in an addition to the very same project, I have another tiny bit to try, but not sure if that is possible.

In this program, I read a file, scan the values process the values and output the result. Again, these files do not necessarily have the have the same format. So to check for different file formats, I scan file and check the number of values: But all the files have a set of required values, apart from some extra ones.

File Format1 : "$PID, 23.4, 34.4, 44 "

C++
 vector<CGeoData> DataTable; // CGeoData is a class containing all the variables 
 CGeoData DataLine;

if (swscanf_s(buffer,  _T("%[^,],%lf,%lf,%lf"),string1, double1, double2, double3) == 4)
      {
     // Add these to the vector class. 
      }


else if (// check for another format)

else if (// check for another format)

else (//print error values.)


Now that done, this works for the formats given so far. But in future if at all there is a completely different format, I don't want that to go into error, but want to save that as a new format.

That is, check if a file matches with a format, if not instead of sending that as an error, check if it contains the variables, if yes, save that as a new format.

Been thinking about this for a while now, but couldn't come up with a solution.Can this be possible?
Posted

You can do this using different methods, at first read the file at once and then, starting from the beginning, start parsing it trying to match given formats that you can have in a XML or any other configuration file.

If you can find that the parsed text format is equal than the proposed template in your configuration file then you should use those values... and of course repeat that operation until reaching the end.

You could use field identification labels and values and then check for those. Depending on the labels then you should use those values in one way or in another...

Your approach makes this non flexible as you will have to use a scanf for each possible method.

If you design it well you could even modify that configuration file without needing to recompile the code and it should work.

Hope this gives you a starting point.

PS: wait a little to see what others propose, mine probably is not the best solution in the world. For sure it would work, but...

Good luck.
 
Share this answer
 
Anything can be possible, but the issues is to know what you are dealing with in the first place. If you know all the possible formats that your data will be presented in then you can create a decision table to call a specific parser for each format type. If you do not know the format of the data then there is very little that you can do in your program apart from throwing an exception on unknown formats.
 
Share this answer
 
In similar situations, I've skipped the scanf method, and have just written a simple parser that reads the line into an array of strings.

Then I run this through a decision engine that determines the format and passes the array to the proper method to process it.

To determine the format, you might look at how many strings are in the line, if there are identifiers, or if certain fields contain numeric or alpha characters...
 
Share this answer
 
Comments
Sumal.V 30-Aug-12 6:26am    
Simple parser like? And would that not do exactly the same thing, but in a different method..
JackDingler 30-Aug-12 8:46am    
Read the whole line in.

std::vector<std::string> sFieldList;

Find the first element, push it into sFieldList.
Find the second element, push into sFieldList.
Keep going until you run out string.

Then look at the elements to figure out what format you have.

The advantage of this, is that the fields are all stored exactly as the are found in the file. No alpha characters are lost in converting to a double prematurely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900