【问题标题】:Picking a person with specified characteristics in C在 C 中挑选具有特定特征的人
【发布时间】:2021-08-06 16:20:57
【问题描述】:

我有一个作业,其中我在 .txt 文件的一行中获得了一个人的信息,以空格分隔 (FIRST_NAME LAST_NAME AGE CITY),我需要打印出来自某个城市和年龄超过18. 所以完整的文件看起来像这样:

FIRST_NAME LAST_NAME 城市年龄

FIRST_NAME2 LAST_NAME2 CITY2 AGE2

FIRST_NAME3 LAST_NAME3 CITY3 AGE3

附:这是错误的 - 它应该是 AGE 然后是 CITY。这是确切的文件截图:

File screenshot

我尝试先将一行分隔为一个句子,然后使用 strtok() 对其进行标记,将每个标记放入相同信息(姓名、姓氏、城市、年龄)的数组中,最后将年龄和城市与请求进行比较那些。但是,我无法将令牌放入数组中。那么,有人可以帮助我以这种方式完成任务(如果它是正确的)或者有人可能有另一种方式吗?

这是现在的代码:

#include <stdio.h>
#include <string.h>
void main()
{
int i=0; //numerer
int pos=0;
char info[100]; // all info about one person
char imena[500];     //names
char prezimena[500];  //surnames
int godine[500];      //years
char mesto[500];     //cities


FILE *pok;
pok=fopen("C:/Users/Trajkovici/Desktop/OsobeFajl.txt","r");
if(pok==NULL)
{
    printf("Greška prilikom otvaranja datoteke!");
}
fscanf(pok,"%[^\n]",&info);



puts("INFO: ");
puts(info);    //dispalying full info
fclose(pok);

char * token = strtok(info, " ");
 
while( token != NULL || i<4) 
{
puts("\nTOKEN:");
printf( " %s\n", token );
token = strtok(NULL, " ");
i++;
}

}

如果这是正确的方法,我怎样才能将令牌放入数组中?

【问题讨论】:

  • 请不要显示纯文本的图片。只需将格式化文本复制并粘贴到您的问题中即可。正确的格式也很重要。不是为了编译器,而是为了读者。特别是对于初学者。您可以使用问题下方的edit 按钮来相应地改进问题。
  • 您是否应该将所有文件内容存储在某个地方?否则,您不需要任何阵列。只需标记您阅读的行以及您需要对其进行的任何操作。不要费心存储它。
  • 只有向 cmets 和答案提供反馈,StackOverflow 才能正常工作。

标签: arrays c string task


【解决方案1】:

由于您除了打印一些数据外没有提及任何内容,因此无需在程序中存储任何内容。

这让事情变得容易多了。 您的程序可能如下所示: (标记了一些错误)

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

// main() has return type int, not void.
// As you need to provide a city for filtering I assume you take it as command line argument

int main(int argv, char *argc[])
{
  if (argc != 2)
  {
    fprintf(stderr, "No city name provided.\n");
    exit(1);
  }

  char *city_filter = argc[1];
  char info[100]; // all info about one person

  FILE *pok = fopen("C:/Users/Trajkovici/Desktop/OsobeFajl.txt","r");
  if (pok==NULL)
  {
    perror("Greška prilikom otvaranja datoteke!");
// If you detect this error you mustn't continue your program as you would use the NULL pointer in functions.
    exit(1);
  }

// You must read more than just one line
// Now we continue as long as we read a value
// You must also provide some limit to prevent buffer overflow
  while (fscanf(pok,"%99[^\n]",&info) == 1)
  {
    puts("INFO: ");
    puts(info);    //dispalying full info

    char *firstname;
    char *lastname;
    char *city;
    char *age_string;

// You must check for errors.
// For readability I skip this. ;)

    firstname = strtok(info, " ");
    lastname = strtok(NULL, " ");
    age_string = strtok(NULL, " ");
    city = strtok(NULL, " ");

    char *end = NULL;
    age = strtod(age_string, &end, 10);

    // Filter output
    // older than 18 and from a certain city...
    if (age > 18 && !strcmp(city, city_filter))
    {
      printf("First: %s, last: %s, age: %d\n", firstname, lastname, age);
    }
  }
  fclose(pok);
}

代码未经测试。

如果您需要存储数据以供以后使用,则必须为所有条目提供存储空间。 为此,您应该定义一个包含一个人的所有数据的结构。 然后制作该结构的数组。 这比为每个字段创建数组更具可读性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 2017-10-21
    • 1970-01-01
    • 2018-04-25
    • 2020-09-21
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多