具有不同文件格式的Scanf功能 [英] Scanf function for different file format

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

问题描述

大家好,
这个问题是我以前发布的问题的一部分: [ ^ ]],但这尤其与swscanf_s函数有关.

在代码中:

Hello All,
This question is a part of the question I had previously posted:[Inconsistent file format[^]] , but this is particularly about swscanf_s function.

In the code :

CStdioFile std;
CString    buffer;
wchar_t str1[2046],str2[2046],str3[2046],str4[2046],str5[2046],str6[2046];
double time, lat;
if (std.Open(fname, CFile::modeRead)) // CStdioFile csf;
{
  while (std.ReadString(buffer) )
  {
    int out = 0;
  out = swscanf_s(buffer, (_T("%[^,],%lf,%lf,%[^,],%lf,%[^,],%d,%d,%lf,%lf,%[^,],%lf,%[^,],,%[^,]"),
 // str1, 2046, &sTime, &db1,str2,2046,&db2,str3,2046,&in1,&in2,&db3,&db4,str4,2046,&db5,str5,2046,str6,2046);
  if(out = 15)
  {
   //calculations
  }
  else
   //output the error buffer lines
  }



由于文件的格式不一定总是相同的,因此某些文件的变量可能不超过3个.因此,我使用了一种在swscanf_s中使用的方法,以便它返回一个字符串.

即;



Since the file is not always of the same format, some files may have more than 3 variable or less. So I used a method to use in the swscanf_s, such that it returns a string.

i.e;

SetValues()
{
  wchar_t str1[2046];
  double time, lat; 
  CString  fmtStr;
  fmtStr.AppendFormat((_T("%[^,],%lf,%lf,%[^,],%lf,%[^,],%d,%d,%lf,%lf,%[^,],%lf,%[^,],,%[^,]"),
   // str1, 2046, &sTime, &db1,str2,2046,&db2,str3,2046,&in1,&in2,&db3,&db4,str4,2046,&db5,str5,2046,str6,2046));  
  return fmtStr;
}


并在swscanf_s函数中使用此函数.像这样:


And use this function in the swscanf_s function. Like this :

out = swscanf_s(buffer, SetValues();


但是我收到一条错误消息:


But I get an error that says:

Expression: ("Incorrect Format specifier", 0)


输入文件格式如下所示:$ GPS1,145217.00,7434.7369647,N,05713.8922390,W,2,09,0.9,44.94,M,21.98,M,43.2,0081 * 55
需要一些帮助


The input file format looks like this: $GPS1,145217.00,7434.7369647,N,05713.8922390,W,2,09,0.9,44.94,M,21.98,M,43.2,0081*55
Need some help please

推荐答案

GPS1,145217.00,7434.7369647,N,05713.8922390,W,2,09,0.9,44.94,M,21.98,M,43.2,0081 * 55
需要一些帮助
GPS1,145217.00,7434.7369647,N,05713.8922390,W,2,09,0.9,44.94,M,21.98,M,43.2,0081*55
Need some help please


问题不在于文件格式的内容!错误消息告诉yu到底是什么问题:格式说明符不一致!您应该打印出SetValues()的返回值.很明显,它返回的字符串不是格式字符串,您在SetValues()内确实做了一些奇怪的事情.请注意,您正在使用已格式化的字符串作为另一个函数调用中的格式字符串:因此,在原始格式字符串中,如果希望在格式化时将其作为单个的%",则应使用双%"字符下次.示例:
The problem is not with the conents of the file format! The error message tells yu exactly what the problem is: Inconsistent Format Specifier! You should print out the return value of SetValues(). Its pretty clear that it returns a string that isn''t a format string, you do something realy weird inside SetValues(). Note that you are using an already formatted string as a format string in another function call: so in your original format string you should use double ''%'' character if you want that to be a single ''%'' when you format next time. Example:
CString s;
s.Format("%%0%dd", 9);
CString t;
t.Format((const char*)s, 1);


在第一个格式字符串中,%d为实数格式说明符,并使用双精度''%:" %% 0 %d d",因此结果为"%09d",它将是下一个格式操作的有效格式字符串.有双重的%"把戏吗?


In the first format string the %d is a real format specifier and the double ''%'': "%%0%dd" so the result will be "%09d" that will be a valid format string for the next format operation. Got the double ''%'' trick?


您正在使用of脚的Microsoft类和C运行时函数的组合,这些类只能在STL到来之前使用.我的总体建议是使用适当的C ++. CString是垃圾,CStdioFile更糟.

您的错误与您要传入的字符串的格式有关,因此我们可能需要查看您正在执行的C函数调用的输入(即使您要使用C,该函数也已过时),知道出了什么问题.
You are using a combination of crappy Microsoft classes that were only meant to be used until the STL arrived, and C runtime functions. My overall recommendation is to use proper C++. CString is rubbish and CStdioFile is worse.

Your error relates to the format of the string you''re passing in, so we probably need to see the inputs to the C function call you''re making ( which is also deprecated, even if you want to use C ), to know what is going wrong.


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

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