01-11-2009 06:36 PM
我使用fmt命令时,程序如下
char a[10];
float data;
fmt (%data,"%f<%s",a);
其中a中是从串口采集到的数据,结果编译通过了,运行时在%data这里停止了,说是变量类型和fmt转换的形式不符合,不知道为什么,请解答,谢谢
Solved! Go to Solution.
01-11-2009 07:07 PM
fmt command
char a[10];
float da;
fmt (%da,"%f<%s",a);
the date from com1 are put in a,when i run the program , the error is
attempt to read beyond the end of the string
i donnot know how can i do about it
01-12-2009 02:52 AM
Since data from your external instruments are put in a string, you must use Scan instead of Fmt to extract values from the string, as in the following example:
char a[10];
float da;
strcpy (a, "123.45");
Scan (a,"%s>%f[b4]", &da);
after the conversion da = 123.4499969
01-12-2009 03:28 AM
thank u
my emample is
char a[10];
double da;
fmt(da,"%f<%s",a);
and the data only use 9 bytes in a, if i define : char a[9], the error" attmept to read beyond the end of the string"appear again
there is another problem
float a;
int b=32;
a=b*2/160;
the value of b is 32, and the value of a is 0.0000
why?
01-12-2009 03:59 AM
Because the variable b, and all associated values in the expression (ie 2 and 160) are integers, the compiler performs calculations using integer mode. You can force it to use floating point mode quite easily:
a = (float) b * 2 / 160;
or:
a = b * 2.0 / 160;
JR
01-12-2009 05:24 AM
thanks
01-12-2009 07:36 AM
greeysid wrote:thank u
my emample is
char a[10];
double da;
fmt(da,"%f<%s",a);
and the data only use 9 bytes in a, if i define : char a[9], the error" attmept to read beyond the end of the string"appear again
Greeysid,
In your case it is preferable to use Scan instead of Fmt: the first instuction scans a string extracting values from it (which is your problem), while the second one is used to format items into a string which is the opposite problem. Even though you could use Fmt to "format" a string representation of a number to a double variable (but the corresponding code should be Fmt (&da, "%f<%s", a); ) it is preferable to use Scan that permits you more control over the conversion.
Regarding stringh lenght, remember that all strings need to be nul-terminated , that is you must allocate one additional byte with respect to the content of the string to permit storing of terminating NULL byte. There are some additional hints regarding this that you can read in the online help, looking for "Scanning strings that are not null-terminated" in the Formatting and I/O library reference.
01-12-2009 08:08 AM
//In your case it is preferable to use Scan instead of Fmt: the first instuction scans a string extracting values from it (which is your problem), while the second one is used to format items into a string which is the opposite problem. Even though you could use Fmt to "format" a string representation of a number to a double variable (but the corresponding code should be Fmt (&da, "%f<%s", a); ) it is preferable to use Scan that permits you more control over the conversion.
Regarding stringh lenght, remember that all strings need to be nul-terminated , that is you must allocate one additional byte with respect to the content of the string to permit storing of terminating NULL byte. There are some additional hints regarding this that you can read in the online help, looking for "Scanning strings that are not null-terminated" in the Formatting and I/O library reference.//
ok i will try to use Scan command
thank u