我在将文件读入包含 enum
的 typedef struct
时遇到问题。我是 C 初学者,所以我真的不知道如何使用枚举读取文件。
我可以用简单的代码读取文件并打印出内容,但我需要读入文件并将行中的每个字符串分配给 typedef 结构中的类型。
输入文件如下所示:
random.num year model category
Example:
54 2012 model1 type1
这些是我的代码的相关部分:
typedef enum { type1, type2, type3, type4} category;
typedef struct {
int num;
int year;
char make[MAX_MAKE_CHARS];
category category; //enum from above
}item;
//reading in the file I have this:
int create_db(char *f){
char file_contents[100]; // read the file --> ("r") FILE *f = fopen(f, "r");
// check if file can be used
if(f == NULL){
printf("File not found");
exit(1);
}
int i = 0; item item_array[100];
while(fscanf(f, "%d %d %s %s", &item[i].num, &item[i].year, item[i].make, item[i].category) != EOF){
// can't get past the while look where it says "item[i].category
我收到错误:
format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘category * {aka enum *}’
由于我对 C 完全陌生,所以我对如何将文件读入结构体感到困惑。我该如何处理item[i].category
?
最佳答案
不幸的是,枚举是一个仅在代码中有效的符号,并且无法在运行时以字符串形式访问该符号。任何enum
变量实际上都存储为整数
但是你可以做一些事情:
- 定义一个
char *
数组,其中包含作为字符串的枚举符号 - 将输入文件中包含的
enum
符号存储在临时字符串中 - 检查
scanf
的返回值 - 调用实用函数在字符串常量数组中搜索临时字符串并返回相应的枚举值
- 如果未找到该字符串,则会引发错误
- 如果找到该字符串,请将枚举值存储在输出结构中
在下面的示例代码中,为了清楚起见,我省略了 while
(您将能够根据问题中未描述的要求将其添加回来):
int tmp_num;
int tmp_year;
char tmp_make[100];
char tmp_category_str[10]; // <-- define the size according to the max enum symbol length
if(fscanf(f, "%d %d %99s %9s", &tmp_num, &tmp_year, tmp_make, tmp_category_str) == 4)
{
int tmp_category;
if( ( tmp_category = check_enum( tmp_category_str ) ) != -1 )
{
Item.num = tmp_num;
Item.year = tmp_year;
strcpy( make, tmp_make );
Item.category = ( category )tmp_category; // The numeric value is stored in the struct
}
else
{
printf( "Invalid 'category' value!\n" );
}
}
请注意:
- 我预计
fscanf
的返回值为 4 - 我将
make
大小更改为 100。目的是以简单的方式显示格式说明符%s
应包含对arr_size-1< 的限制
字符,为了安全 - 如果提供了与任何枚举符号都不匹配的奇怪字符串值,
check_enum()
将返回 -1
现在,仅缺少 check_enum()
实现。只需在 enumSymbols[]
元素上循环搜索输入字符串即可。
char* enumSymbols[] = { "type1", "type2", "type3", "type4" };
int check_enum(char * str)
{
int ret = -1;
if( str )
{
for( int i=0; i<(sizeof(enumSymbols)/sizeof(enumSymbols[0])); i++ )
{
if( strcmp(enumSymbols[i], str) == 0 )
{
ret = i;
break;
}
}
}
return ret;
}
注意:这可行,但您必须保持数组和枚举对齐。为了防止人为对齐错误,像this one这样的解决方案可以实现。在这个答案中,我解释了如何创建对齐的 enum/struct 对,但以相同的方式并使用 preprocessor stringifier operator #
还可以生成对齐的枚举/字符串数组对。
关于c - 将文件读入包含 C 中枚举的 typedef 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63850112/