问题描述
大家好,
这个问题是我以前发布的问题的一部分: [ [ ^ ]],但这尤其与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
推荐答案
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?
这篇关于具有不同文件格式的Scanf功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!