不同的文件格式 [英] Different File Formats

查看:99
本文介绍了不同的文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经在这个项目上工作了一段时间,并且也提出了问题并获得了解决方案.但是除了同一个项目外,我还有一点点尝试,但不确定是否可行.

在此程序中,我读取了一个文件,扫描值处理值并输出结果.同样,这些文件不一定具有相同的格式.因此,要检查不同的文件格式,我将扫描文件并检查值的数量:但是,除了一些额外的值之外,所有文件都有一组必需的值.

档案格式1:"$ PID,23.4、34.4、44"

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 "

 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?

推荐答案

PID,23.4,34.4,44"

PID, 23.4, 34.4, 44 "

 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?


您可以使用不同的方法来执行此操作,首先请阅读以下位置的文件:从头开始,一次又一次地解析它,尝试匹配XML或任何其他配置文件中可以给定的格式.

如果您发现解析后的文本格式与配置文件中建议的模板相同,则应使用这些值...当然,重复该操作直到结束.

您可以使用字段标识标签和值,然后进行检查.然后,根据标签,您应该以一种或另一种方式使用这些值...

您的方法使这种方法变得不灵活,因为您必须为每种可能的方法使用scanf.

如果设计得当,您甚至可以修改该配置文件而无需重新编译代码,它应该可以工作.

希望这可以为您提供一个起点.

PS:请稍等一下,看看其他人的建议,我的解决方案可能不是世界上最好的解决方案.当然可以,但是...

祝你好运.
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.


一切皆有可能,但是问题是首先要知道你在处理什么.如果您知道数据将以所有可能的格式显示,则可以创建一个决策表来为每种格式类型调用特定的解析器.如果您不知道数据的格式,那么除了在未知格式上引发异常外,您在程序中几乎无能为力.
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.


这篇关于不同的文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆