【问题标题】:Adding integer to array of structures after using strtok使用 strtok 后将整数添加到结构数组
【发布时间】:2014-07-08 22:43:27
【问题描述】:

我在向我的结构数组中添加整数时遇到问题,我不知道如何解决它。结构看起来像

struct info{
    char name[20];
    int course[5];
};

我使用 fgets 读取文件,然后使用 strtok() 标记该行,我相信它返回类型 char* 类型,因此读入的标记是 char* 类型的 1105。我尝试通过使用 atoi 将令牌转换为 int 类型然后将其存储到 struct_array[index].course = number; 来将其添加到我的结构数组中,但最终在编译时出现此错误。

hw4bcopy.c: In function ‘create_structures’:
hw4bcopy.c:48:44: error: incompatible types when assigning to type ‘int[5]’ from type ‘int’
                 struct_array[index].course = number;

我能够成功地将一个字符串输入到结构数组中,但声明有点不同,所以也许这就是原因。将字符串输入结构数组的声明是strcpy(struct_array[index].name,token);,所以我想我尝试将整数放入结构中的方式可能不正确?

在我尝试将令牌添加为整数之前,代码一切正常。

我正在做的是

  • 打开文件
  • 向函数发送文件
  • tokenize 以获取名称的标记,例如 Edward1105
  • 将标记添加到数组结构中

代码是

struct info{
    char name[20];
    int course[5];
};


void create_structures(FILE* file);

int main()
{
    FILE* fp = fopen("input-hw04b.txt","r");
    FILE* nf = fopen("out2.txt","w+");
    create_structures(fp);

}

void create_structures(FILE* file)
{
    struct info struct_array[30];
    char buffer[100];
    char* del = " .";
    char* token;
    int number,count;int index = 0;
    while(fgets(buffer,sizeof(buffer),file) != NULL)
    {
        count = 0;
        token = strtok(buffer,del);
        while(token != NULL)
        {
            if(count == 0)
            {
                strcpy(struct_array[index].name,token);
            }
            if(count == 5)
            {
                number = atoi(token);
                struct_array[index].course = number;
            }
            token = strtok(NULL,del);
            count = count + 1;
        }
        index = index +1;
    }
    int z;
    for (z = 0; z < index; z++)
        printf("%s %s\n",struct_array[z].name,struct_array[z].course);

【问题讨论】:

  • int course[5]; --> int course;
  • 哇,谢谢,忘记改了
  • 你真的应该做类似strncpy(struct_array[index].name, token, 19); 这样的事情,这样你就不会超出你的名字范围。 (告诉它不要复制超过 19 个字符,因为我们需要字符串末尾的 '\0' 空间)

标签: c types structure


【解决方案1】:

在字符串中查找整数的方式是错误的,不要使用仅适用于可预测内容的计数器,而是使用 ctype.h 标头中定义的 isdigit()isprint() 等函数。我不知道您要读取的文件的布局,但我想它具有以下结构:

爱德华 .1105 .1320 .7465 .9672 .0123

史蒂夫 .2612 .0567 .7242 .87136 .1423

您可以自己尝试我制作的这个新版本的代码,它适用于这种布局并且只有一个名称,例如:

爱德华 .1105 .1320 .史蒂夫 .9672 .0123

会产生错误的输出,因为我们只存储一个名称,如果我们不改变数组的大小,我们就无法修改文本文件中的 int 数量;我认为它只需要另一个计数器变量,顺便说一句,这是代码:

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

#define SIZE 5

struct info{
    char name[20];
    int course[SIZE];
};




void create_structures(FILE* file)
{
    struct info struct_array[30];
    char * buffer = malloc(sizeof(struct info)* 3);
    char* del = " .";
    char* token;
    int index = 0, c = 0;

    while(fgets(buffer,100,file))
    {
      for(token = strtok(buffer,del); token != NULL; token = strtok(NULL, del))
        {

            if(isprint(token[0]) && isdigit(token[0]) == 0)
            {
                strcpy(struct_array[index].name,token);
            }
            if(isdigit(token[0]))
            {
                struct_array[index].course[c] = atoi(token);
                c++;
            }
        }
        index = index +1;
        c = 0;
    }
    for (int z = 0; z < index; z++)
    {
        printf("%s ",struct_array[z].name);
          for(int y = 0 ; y < SIZE ; y++)
           {
             printf("%d ",struct_array[z].course[y]);
           }
          printf("\n");
    }
  }

    int main()
      {
          FILE* fp = fopen("input-hw04b.txt","r");
      //  FILE* nf = fopen("out2.txt","w+");
          create_structures(fp);
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多