如何将字符串数组从文件输入存储到C中的struct元素中?

时间:2018-10-09 21:18:56

标签: c pointers struct file-io

所以我有这个文本文件(cartCoords.txt):

a = 2b, 1d; milk cheese eggs
b = 2a, 1e, 2c;
c = 2b, 1f;
d = 1a, 1g;
e = 1h, 1b;
f = 1i, 1c;
g = 1j, 1d;
h = 1k, 1e;
i = 1l, 1f;
j = 1m, 1g;
k = 1n, 1h;
l = 1o, 1i;
m = 2n, 1j;
n = 2m, 1k, 2o;
o = S, 2n, 1l;

这些代表我的地图布局版本。点“ a”与“ b”相距2个单位,与点“ d”相距1个单位,在点a,我们具有“牛奶”,“奶酪”和“鸡蛋”项。

我已经构建了一个结构:

struct stopPoints {
   char **items;
   char connectingPoints[10];
   int  weights[10];
   int startBool;
};

因此,每个“停止点”(a,b,c ...等)都包含项列表(牛奶,奶酪等),连接点的列表(2b和1d中的b和d),每个对应的连接点(2b和1d中的2和1)的“权重”,以及表示是否是起点的布尔值(用“ S”表示)。

我已经能够存储每个连接点及其在每个点的结构中的权重,但是我无法将每个单独的项目字符串存储到数组中。我想为每个结构创建一个数组,用于存储每个索引中的每个项目,例如:

["milk", "cheese", "eggs"]

我该怎么做?

Below is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10

struct stopPoints {
   char **theItem;
   char connectingPoints[10];
   int  weights[10];
   int startBool;
};


struct stopPoints makeStruct(char* str)
{
    char items[10];
    int i,j=0,k=0, l=0, semiCheck =0, a;
    struct stopPoints point;

    for(i=4; i<strlen(str); i++)
    {
        //check if its a starting point
        if(i==4 && str[i] == 'S')
        {
            point.startBool = 1;
            i+=3;
        }
        else if(i==4)
        {
            point.startBool = 0;
        }

        //if there's no semicolon present, proceed with the code
       if (semiCheck == 0) {
            //checks if its a number
            if(str[i] < 58 && str[i] > 47){
                point.weights[j] = str[i];
                j++;
            }
            //checks if its a letter
            else if(str[i] < 122 && str[i] > 96){
                point.connectingPoints[k] = str[i];
                k++;
            }

            else if (str[i] == 59){
                semiCheck = 1;
                i++;
                }
            }
        else if(semiCheck==1){
             for (a=1; a<(strlen(str)-i); a++){
                if (str[i+a] != 32){
                    items[a-1] = str[i+a];
                }
        }

       }

    }
    return point;
}

int main(){
    int selections[100];
    int numOfItems,x,y,j,z,temp;
    int i = 0;
    struct stopPoints store[26];

    FILE *fp;
    char str[60];

    /* opening file for reading */
    fp = fopen("cartCoords.txt" , "r");
    if(fp == NULL) {
      perror("Error opening file");
      return(-1);
    }

    while( fgets (str, 600, fp)!= NULL ) {
      /* writing content to stdout */
      //printf("test1");
      store[i] = makeStruct(str);
      i++;
      printf("store[%d] connecting points = %c and %c and %c\n", i-1, store[i-1].connectingPoints[0], store[i-1].connectingPoints[1], store[i-1].connectingPoints[2]);
    }
    fclose(fp);

    return 0;
}

该程序的输出为:

store[0] connecting points = b and d and
store[1] connecting points = a and e and c
store[2] connecting points = b and f and
store[3] connecting points = a and g and
store[4] connecting points = h and b and
store[5] connecting points = i and c and
store[6] connecting points = j and d and
store[7] connecting points = k and e and
store[8] connecting points = l and f and
store[9] connecting points = m and g and
store[10] connecting points = n and h and
store[11] connecting points = o and i and
store[12] connecting points = n and j and
store[13] connecting points = m and k and o
store[14] connecting points = n and l and

因此,我已经能够正确存储字母和数字。我只是不知道如何获取完整的字符串并将其存储到数组中。我的方法是让我的循环检查分号(使用其ascii值),一旦检测到分号,它将开始收集每个单独的字符串。唯一的问题是我不知道如何实现。

0 个答案:

没有答案