0

I have a struct like this:

struct vertices{
                  char x[20];
                  char y[20];
                  char z[20];
                 };

and then in main I have an array of the struct vertices like this:

struct vertices vert[64];

plus an array of char in my main function like this:

char exampleArray[200]="v 23.232000 32.33000 2.03900\nv 9.20900 3.29000 1.0002\n";

so what I want to do is to parse this big array containing a hundred or so vertices, and store each value in the corresponding x,y,z char arrays of the struct vertices, which later I'm gonna simply convert by calling on float x=atof(vert[1].x) and draw each one.

But the problem is that I can't store , copy, concat characters from the raw array to the x,y,z arrays by any ways, i tried to do it by vert[0].x=exampleArray[0], vert[0].x=exampleArray+i etc inside different conditions like

if(SpaceCount==1)
       {
              vert[0].x[0]=exampleArray[i];
              i++;

        }

but all this doesn't work.

I tried so many variations, couldn't list them all. But the thing I want is to parse the exampleArray which has my vertices in raw format, every 3D vertix has spaces in between them, every vertix starts with a char V followed by a space, the first point is the X then followed by a space and then the Y point and after the Z point there is newline \n character and then again a V for a new vertix.

shinwari_afg
  • 104
  • 11
  • `vert[0].x` is a `char[20]` and `exampleArray[0]` is a `char`. I think you're trying to copy strings, in which case you need to use something like [`strcpy`](https://linux.die.net/man/3/strcpy) – yano Feb 27 '18 at 22:32
  • you can use strcpy to copy a char array into another. and for splitting the string into array use strtoken – Udayraj Deshmukh Feb 27 '18 at 22:35
  • I tried strcat, strcpy etc.....that's the issue, i simply want to parse the exampleArray and get the X,Y,Z points from it, and copy them into Vert[0].x, or Vert[1].X or Vert[2].Y etc. and I know exampleArray[i] is a Char but it is inside a loop , which means i'm trying copy character by character into vert[0].x for example. – shinwari_afg Feb 27 '18 at 22:36
  • What exactly did you try and how did it fail? – melpomene Feb 27 '18 at 22:37
  • Your `exampleArray` contents don't match your description of the format. – melpomene Feb 27 '18 at 22:39
  • I'm sorry that my question couldn't be understand by you guys, it is 4:00am, I've not slept, very tired and i'm working on multiple projects, and I have two different interviews at different places today as well...My supervisor has also given me some work to do....and right now these questions from you guys feels like bullets coming in and out from my brain....all i want now is die. :( – shinwari_afg Feb 27 '18 at 22:45
  • I only see a single question (from me), so I don't understand what you mean by "*these questions from you guys*". Also, I'm only asking for basic information that should've been part of your question, nothing crazy. – melpomene Feb 27 '18 at 22:53

2 Answers2

1

You could use strtok() with delimiter " \n" to get each piece and then strcpy() to place each token in your struct.

struct vertices {
    char x[20];
    char y[20];
    char z[20];
};

int main(void)
{
    char exampleArray[200] = "v 23.232000 32.33000 2.03900\nv 9.20900 3.29000 1.0002";
    char *toStrtokVertex, *token;
    char delim[5] = " \n";
    struct vertices V[10];
    int i = 0;

    toStrtokVertex = exampleArray;
    while ((token = strtok(toStrtokVertex, delim)) != NULL) {

        // ignore the `v`

        // get x
        if ((token = strtok(NULL, delim)) != NULL) {
            strcpy(V[i].x, token);
        }
        else {
            printf("INPUT ERROR\n");
            exit(1);
        }

        // get y
        if ((token = strtok(NULL, delim)) != NULL) {
            strcpy(V[i].y, token);
        }
        else {
            printf("INPUT ERROR\n");
            exit(1);
        }

        // get z
        if ((token = strtok(NULL, delim)) != NULL) {
            strcpy(V[i].z, token);
        }
        else {
            printf("INPUT ERROR\n");
            exit(1);
        }

        // switch this to NULL to keep parsing the same string
        toStrtokVertex = NULL;
        i++;
    }

    for (int j = 0; j < i; j++) {
        printf("V[%d] (%s, %s, %s)\n", j, V[j].x, V[j].y, V[j].z);
    }
}

Note that I modified your input value to match your description. You had

"v 23.232000 32.33000 2.03900\n 9.20900 3.29000 1.0002"

which did not have a v after the \n to start the next vertex, so I added a v

"v 23.232000 32.33000 2.03900\nv 9.20900 3.29000 1.0002"
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
  • Thank you so much, it is what I needed. I was tired and couldn't write a well explained question, I didn't even had the energy to copy the code sample from my program, but you got the idea of what I wanted to do. Thank you (y) – shinwari_afg Feb 28 '18 at 16:15
0

Character arrays are not copied by simply pointing at them. The function "strcpy(vert[0].x, &exampleArray[2])" is needed, but even then there is no '\0' immediately after that first number to indicate where to stop reading. "memcpy" does not need a NULL ('\0') but needs a length based on spaces.

Further, the "atof" operation to plan to use requires a NULL terminator as well.

I am concerned with your "space planning". You have 64 copies of structures that will each require 60 characters (3840 total), but your exampleArray only has 200. And the input string exampleArray has strings for numbers that must be no longer than 19 digits/decimals because you need the NULL terminator in each of x/y/z to exist.

This is the pure technical evaluation of code issues, but your original post does not show more of the application design. Help us understand your application design and more advice can be given.

Ozymandias
  • 36
  • 2
  • These are just examples, actual data is quite larger than this, space is taken care of, i'm not worried about the atof either...everything works fine...the only problem i'm facing is that i can't parse the example array to copy character by character x, y, z points into their appropriate array which is the struct Vertices which has x,y,z char arrays inside it. – shinwari_afg Feb 27 '18 at 22:50