简体   繁体   English

有没有办法将数组项分配给结构值?

[英]Is there a way of assigning array items to struct values?

so what I have is the following array:所以我有以下数组:

10
1 250 350 50
2 525 200 80
3 425 700 60
4 675 475 65
5 850 850 40
6 925 200 90
7 1050 575 80
8 1250 800 70
9 1375 400 60
10 1500 650 40

Each lines' value means something different, per instance每条线的值意味着不同的东西,每个实例

1   250  350    50
id  lat  long  value

I want to assign each of those line values to a structure so I can play with them, but after googling and coming up with the graph theory (which is kind of similar to what I am trying to do) nothing worked... I may say that this array is being pulled from a text file, whether that is or not relevant.我想将这些线值中的每一个分配给一个结构,以便我可以使用它们,但是在谷歌搜索并提出图论(这有点类似于我正在尝试做的事情)之后,没有任何效果......我可能说这个数组是从文本文件中提取的,无论是否相关。

struct population{
    int id;
    int latitude;
    int longitude;
    int value;
};

I can't come up with any solution, can anyone help me out or at least provide some tutorials or articles to help me clear my mind?我想不出任何解决方案,任何人都可以帮助我,或者至少提供一些教程或文章来帮助我理清思路吗?

Here is my code:这是我的代码:

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

#define RSIZE 20
#define CSIZE 11

struct population{
    int id;
    int latitude;
    int longitude;
    int value;
};


int main(void) 
{
    char line[CSIZE][RSIZE];
    char fname[20];
    FILE *fptr = NULL; 
    int i = 0;
    int tot = 0;
    printf("\n\n Read the file and store the lines into an array :\n");
    printf("------------------------------------------------------\n"); 
    printf(" Input the filename to be opened : ");
    scanf("%s",fname);  

    fptr = fopen("cord.txt", "r");
    while(fgets(line[i], RSIZE, fptr)) 
    {
        line[i][strlen(line[i]) - 1] = '\0';
        i++;
    }
    tot = i;
    printf("\n The contents of the file %s  are : \n",fname);    
    for(i = 0; i < tot; ++i)
    {
        printf(" %s\n", line[i]);
    }
    printf("\n");
    return 0;
}

There is no ready-to-use API for that purpose.为此目的,没有现成的 API。 If you want to assign line[i] to a struct population , you will need to split up line[i] to individual tokens (4 of them) - strtok may help with that.如果要将line[i]分配给 struct population ,则需要将line[i]拆分为单个标记(其中 4 个)- strtok可能会对此有所帮助。 Then you need to convert each item to individual int - atoi may help with that.然后您需要将每个项目转换为单独的int - atoi可能会有所帮助。

A good way to read, validate and save a line of file text into a struct is to use a helper function with fgets() and sscanf() @kaylum .读取、验证一行文件文本并将其保存到struct中的一个好方法是使用带有fgets()sscanf() @kaylum 的帮助器 function Use " %n" to detect successful parsing completion.使用" %n"来检测成功的解析完成。

#define INT_TEXT_SIZE 12 // INT_MIN
#define POPULAITON_LINE_SIZE ((INT_TEXT_SIZE + 1 /* separator */)*4 + 1)

// Return: 
// 1 on success
// 0 on failure
// EOF on end-of-file/input error
int population_read(struct population *p, fptr) {
  // Be generous in buffer size, suggest 2x max expected
  char buf[POPULAITON_LINE_SIZE * 2];
  if (fgets(buf, sizeof buf, fptr) == NULL) {
    return EOF;
  }

  int n = 0;
  // Use %n to store offset 
  sscanf(buf, "%d%d%d%d %n", &p->id, 
      &p->latitude, &p->longitude, &p->value, &n);

  if (n == 0 || buf[n] || n > POPULAITON_LINE_SIZE) {
    // Parsing failed, junk at the end or very long
    return 0;
  }

  // Maybe some range/value tests per member
  if (p->latitude < LATITUDE_MIN || p->latitude > LATITUDE_MAX) {
    return 0;
  }
  ... // likewise for other members.

  return 1;
}

Usage example:使用示例:

struct population pop[CSIZE];

int count;
for (count = 0; count < CSIZE; count++) {
  int result = population_read(&pop[count], fptr));
  if (result == 0) {
    report_error_with_TBD_code();
    return -1;
  }
  if (result != 1) {
    break;
  }
}

for (int c = 0; c < count; c++) {
  // Use pop[c];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM