在 C 中扫描文件
Scanning through a file in C
所以我的文本文档中有以下内容:
7
PERSON Sarah 20 5 50
PERSON Jordan 30 10 30
PERSON James 25 42 56.50
GROUP STEEL 2
Martin 21 5 80.50
Martha 25 10 79.75
PERSON Hector 38 10 100.50
PERSON Charles 18 5 35
GROUP LUMBER 3
Jill 19 5 91
Aaron 25 10 98
Mark 28 42 152.80
所以我试图让我的代码循环遍历此处的所有名称并打印出信息。名字代表人名,第一个数字代表年龄,第二个数字代表公里数运行,第三个数字代表募集金额。 PERSON 代表一个人,而 group 代表多个人。第一行的7代表PERSONS和GROUPS的总数;有 5 个标记为 PERSON 的人和 2 个标记为 GROUP 的组。
但是,我的代码只打印出每个人的信息,标签为 PERSON。我怎样才能让我的代码打印组的名称及其成员的信息?这是我的代码(不完整,但为了展示我正在尝试做的事情):
FILE * ifp = fopen("race01.txt", "r");
int numPeople, i, num = 1;
struct person individual;
fscanf(ifp, "%d", &numPeople);
for(i = 0; i < numPeople; i++) {
fscanf(ifp, "%s %s %d %d %f", individual.name, individual.name, &individual.age, &individual.event, &individual.money);
if (individual.event == 42) {
printf("%s registered for the marathon race! They have been assigned the number %d\n", individual.name, num);
}
else {
printf("%s registered for the %dk race! They have been assigned the number %d\n", individual.name, individual.event, num);
}
num++;
}
我尝试了各种变体,从嵌套的 for 循环到额外的扫描线,但似乎没有什么可以涵盖 "groups"。
正如 Jonathan 所说,您应该检查您读取的第一个字符串是“PERSON”还是“GROUP”。如果它等于“PERSON”,您将读取有关此人的信息(姓名、年龄等...),如果它等于“GROUP”,您应该读取该组中的人数,然后读取该组中每个人的信息团体。使用像“strcmp”这样的函数来比较两个字符串。
我在下面有您的代码的修改版本,它编译并且工作正常。
我创建了一个名为“print_raceinfo”的函数,它打印关于一个人的信息
#include<stdio.h>
#include<math.h>
#define NAME_MAX 25
struct person {
char name[NAME_MAX];
int age;
int event;
float money;
};
void print_raceinfo(struct person individual, int num)
{
if (individual.event == 42) {
printf("%s registered for the marathon race! They have been assigned the number %d\n", individual.name, num);
}
else {
printf("%s registered for the %dk race! They have been assigned the number %d\n", individual.name, individual.event, num);
}
}
int main(){
FILE *fp;
int numPeople, i, num = 1;
struct person individual;
char person_or_group[NAME_MAX];
char group_name[NAME_MAX];
int numpeople_in_group, j;
fp = fopen("F://repoexample/race.txt", "r+");
fscanf(fp, "%d", &numPeople);
for(i = 0; i < numPeople; i++) {
fscanf(fp, "%s", person_or_group);
//Compare the first 6 characters of string person_or_group with "PERSON"
if (strncmp(person_or_group, "PERSON", 6) == 0) {
fscanf(fp, "%s %d %d %f", individual.name, &individual.age, &individual.event, &individual.money);
print_raceinfo(individual, num);
num++;
}
else if (strncmp(person_or_group, "GROUP", 5) == 0){
//Read group name and number of people in that group
fscanf(fp, "%s %d", group_name, &numpeople_in_group);
for (j = 0; j<numpeople_in_group; j++) {
//Extract information for each person in that group
fscanf(fp, "%s %d %d %f", individual.name, &individual.age, &individual.event, &individual.money);
print_raceinfo(individual, num);
num++;
}
}
}
printf("Total number of persons = %d\n", num - 1);
return 0;
}
输出:
所以我的文本文档中有以下内容:
7
PERSON Sarah 20 5 50
PERSON Jordan 30 10 30
PERSON James 25 42 56.50
GROUP STEEL 2
Martin 21 5 80.50
Martha 25 10 79.75
PERSON Hector 38 10 100.50
PERSON Charles 18 5 35
GROUP LUMBER 3
Jill 19 5 91
Aaron 25 10 98
Mark 28 42 152.80
所以我试图让我的代码循环遍历此处的所有名称并打印出信息。名字代表人名,第一个数字代表年龄,第二个数字代表公里数运行,第三个数字代表募集金额。 PERSON 代表一个人,而 group 代表多个人。第一行的7代表PERSONS和GROUPS的总数;有 5 个标记为 PERSON 的人和 2 个标记为 GROUP 的组。
但是,我的代码只打印出每个人的信息,标签为 PERSON。我怎样才能让我的代码打印组的名称及其成员的信息?这是我的代码(不完整,但为了展示我正在尝试做的事情):
FILE * ifp = fopen("race01.txt", "r");
int numPeople, i, num = 1;
struct person individual;
fscanf(ifp, "%d", &numPeople);
for(i = 0; i < numPeople; i++) {
fscanf(ifp, "%s %s %d %d %f", individual.name, individual.name, &individual.age, &individual.event, &individual.money);
if (individual.event == 42) {
printf("%s registered for the marathon race! They have been assigned the number %d\n", individual.name, num);
}
else {
printf("%s registered for the %dk race! They have been assigned the number %d\n", individual.name, individual.event, num);
}
num++;
}
我尝试了各种变体,从嵌套的 for 循环到额外的扫描线,但似乎没有什么可以涵盖 "groups"。
正如 Jonathan 所说,您应该检查您读取的第一个字符串是“PERSON”还是“GROUP”。如果它等于“PERSON”,您将读取有关此人的信息(姓名、年龄等...),如果它等于“GROUP”,您应该读取该组中的人数,然后读取该组中每个人的信息团体。使用像“strcmp”这样的函数来比较两个字符串。 我在下面有您的代码的修改版本,它编译并且工作正常。 我创建了一个名为“print_raceinfo”的函数,它打印关于一个人的信息
#include<stdio.h>
#include<math.h>
#define NAME_MAX 25
struct person {
char name[NAME_MAX];
int age;
int event;
float money;
};
void print_raceinfo(struct person individual, int num)
{
if (individual.event == 42) {
printf("%s registered for the marathon race! They have been assigned the number %d\n", individual.name, num);
}
else {
printf("%s registered for the %dk race! They have been assigned the number %d\n", individual.name, individual.event, num);
}
}
int main(){
FILE *fp;
int numPeople, i, num = 1;
struct person individual;
char person_or_group[NAME_MAX];
char group_name[NAME_MAX];
int numpeople_in_group, j;
fp = fopen("F://repoexample/race.txt", "r+");
fscanf(fp, "%d", &numPeople);
for(i = 0; i < numPeople; i++) {
fscanf(fp, "%s", person_or_group);
//Compare the first 6 characters of string person_or_group with "PERSON"
if (strncmp(person_or_group, "PERSON", 6) == 0) {
fscanf(fp, "%s %d %d %f", individual.name, &individual.age, &individual.event, &individual.money);
print_raceinfo(individual, num);
num++;
}
else if (strncmp(person_or_group, "GROUP", 5) == 0){
//Read group name and number of people in that group
fscanf(fp, "%s %d", group_name, &numpeople_in_group);
for (j = 0; j<numpeople_in_group; j++) {
//Extract information for each person in that group
fscanf(fp, "%s %d %d %f", individual.name, &individual.age, &individual.event, &individual.money);
print_raceinfo(individual, num);
num++;
}
}
}
printf("Total number of persons = %d\n", num - 1);
return 0;
}
输出: