[英]c fscanf storing strings
I am using fscanf to read in a file with this format per line:我正在使用 fscanf 以每行这种格式读入一个文件:
//FEATURE_ID|FEATURE_NAME|FEATURE_CLASS|STATE_ALPHA|STATE_NUMERIC|COUNTY_NAME|COUNTY_NUMERIC|PRIMARY_LAT_DMS|PRIM_LONG_DMS|PRIM_LAT_DEC|PRIM_LONG_DEC|SOURCE_LAT_DMS|SOURCE_LONG_DMS|SOURCE_LAT_DEC|SOURCE_LONG_DEC|ELEV_IN_M|ELEV_IN_FT|MAP_NAME|DATE_CREATED|DATE_EDITED
Here is the first line of the file, for example:这是文件的第一行,例如:
//913064|Oso Spring|Spring|NM|35|Rio Arriba|039|365418N|1061159W|36.904945|-106.1998199|||||2863|9393|Bighorn Peak|12/01/1991|04/19/2011
My code is this:我的代码是这样的:
int id, state_num, county_num, elev_m, elev_f;
float lat_dec, lon_dec;
char* name[25];
char* class[25];
char* state[25];
char* county[25];
char* lat[25];
char* lon[25];
char* s_lat_dms[25];
char* s_lon_dms[25];
char* s_lat_dec[25];
char* s_lon_dec[25];
char* map_name[25];
char* date_created[25];
char* date_edited[25];
fscanf(data, "%d|%[^|]s|%s|%s|%d|%s|%d|%s|%s|%f|%f|%s|%s|%s|%s|%d|%d|%s|%s|%s", &id, name,
class, state, &state_num, county, &county_num, lat, lon, &lat_dec,
&lon_dec, s_lat_dms, s_lon_dms, s_lat_dec, s_lon_dec, &elev_m, &elev_f, map_name,
date_created, date_edited);
printf("id:%d \nname:%s \nclass:%s \nstate:%s \nstate_num:%d \n", id, name, class, state,
state_num);
But when i run it on the command line it prints out this:但是当我在命令行上运行它时,它会打印出:
id:913064编号:913064
name:Oso Spring名称:奥索泉
class:�&6+� 班级:�&6+�
state:状态:
state_num:1178 state_num:1178
Why is it not properly storing data in the class char* array?为什么它没有正确地将数据存储在类 char* 数组中? There are some specific bits of the code that need to be kept, like the first string value needs to be able to keep the whitespace, so I added the "[^|]", but once I did this it started inputting nonsense characters in class.
有一些特定的代码需要保留,比如第一个字符串值需要能够保留空格,所以我添加了“[^|]”,但是一旦我这样做了,它就开始输入无意义的字符班级。 I have looked extensively into fscanf but cannot find how to correctly do what I am trying to do.
我已经广泛研究了 fscanf 但找不到如何正确地做我想做的事情。 Could someone help me organize this properly, and fill the knowledge hole I cannot seem to fill?
有人能帮我妥善组织一下,填补我似乎无法填补的知识空白吗?
There's no format specifier %[...]s
.没有格式说明符
%[...]s
。 There's only %[...]
.只有
%[...]
。 Drop the s
, as it causes the fscanf
function to look for a literal s
in the input.删除
s
,因为它会导致fscanf
函数在输入中查找文字s
。
And as mentioned you have arrays of pointers, not arrays of characters.如前所述,您有指针数组,而不是字符数组。 That means eg
state
as argument will decay to a pointer to its first element, which in turn is a pointer, and it will have the type char **
which is incorrect for both the %[
and the %s
formats.这意味着例如
state
作为参数将衰减为指向其第一个元素的指针,该元素又是一个指针,并且它将具有char **
类型,这对于%[
和%s
格式都是不正确的。
The array of pointer issue will also lead to problem when calling printf
.指针数组问题也会导致调用
printf
时出现问题。
A good compiler would warn about the mismatching type and format specifier.一个好的编译器会警告不匹配的类型和格式说明符。 If yours don't then try to enable more warnings (using eg
-Wall -Wextra
with GCC or Clang).如果您不这样做,请尝试启用更多警告(例如,将
-Wall -Wextra
与 GCC 或 Clang 一起使用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.