C 使用结构的成员作为验证

C 使用结构的成员作为验证,c,excel,C,Excel,我必须从excel文件中读取列,以便输出它的统计信息。我认为要做到这一点,我必须首先创建一个结构。到目前为止,我只有这些 #include <stdio.h> #include <stdlib.h> /* Structure to store all the data that is required */ typedef struct{ char* id; int entryCount; int lik

我必须从excel文件中读取列,以便输出它的统计信息。我认为要做到这一点,我必须首先创建一个
结构。到目前为止,我只有这些

#include <stdio.h>
#include <stdlib.h>


    /* Structure to store all the data that is required */
    typedef struct{
        char* id;
        int entryCount;
        int like;
        char gender[7];
    } statistics;
#包括
#包括
/*结构来存储所需的所有数据*/
类型定义结构{
字符*id;
int入口计数;
int-like;
性别[7];
}统计数字;
excel文件是一个类似facebook的计数和成员姓名、ID和性别列表。我的问题是,我如何使用
if
语句来验证数据,并找出相似的计数是否属于
男性
女性
性别

任何帮助都将不胜感激


编辑:使问题更清楚(我希望如此)。

为比较字符串(实际上是字符数组,以
'\0'
结尾)创建if的方法是使用正确的函数

下面是一个使用
strncmp(…)
的示例

出于学习目的,我建议进行实验。
例如,尝试将结构中的字符串更改为“女性”和“男性”。
在这种情况下,我编写代码的方式将产生启发性的结果。:-)

(归功于Idlle001)

除了
typedef
s和
if
语句之外,还有更多的代码从excel文件中读取内容。。。也许你会
strcmp
gender
“male”
“female”
-但请注意
char-gender[6]
太小,无法容纳
“female”
+空终止符。你的问题不太清楚。在表中每行实例化一个结构后,您的目标到底是什么?当您计算将附加在末尾的空字符“\0”时,
gender[6]
将不包含
female
。@Idle001
strncmp
优先。更安全@Noob这里您更关心的应该是如何解析excel文件。一个简单的方法是使用excel到csv解析器读取文件,写入csv文件,然后使用类似的解析器将输出文件转换为excel。谢谢您的回答。因此,
statistics示例male={“1”,2,0,“male”}是我输入的数据吗?如何使用fopen从文件中获取统计信息?另外,if语句中的7是什么意思?是的,它模拟您以任何喜欢的方法读取的数据。通过使用fopen()获得它,是的,但随后使用函数读取文件()。7是strncmp()的第三个参数,请阅读规范(例如),并可能与代码中的其他数字进行比较。;-)谢谢你的消息来源。如果7是要比较的字符数,那么第一个
If
语句不应该等于7吗?7是要比较的最大字符数。它对于避免函数在所有内存中疯狂运行非常有用。如果逻辑上需要,它还可以仅用于比较较长字符串的前几个字符。顺便说一句,使用7包括
'\0'
,在这种情况下,它可以被视为可选的,例如“femaleaux”超出了范围。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* Structure to store all the data that is required */
typedef struct{
    char* id;
    int entryCount;
    int like;
    char gender[7];
} statistics;

int main(void)
{
    statistics ExampleMale={"1", 2, 0, "male"};
    statistics ExampleFemale={"0", 1, 0, "female"};

    // Whatever you do to fill your structs is simulated by init above.
    if(strncmp(ExampleMale.gender,"male", 7))
    {
        printf("Male looks female.\n");
    } else
    {
        printf("Male looks male.\n");           
    }

    if(strncmp(ExampleFemale.gender,"female",7))
    {
        printf("Female looks male.\n");
    } else
    {
        printf("Female looks female.\n");           
    }

    return 0;

}
Male looks male.
Female looks female.