1

I want to read the following lines from STDIN and save the values in c:

A:2
B:3
C:AAAA1
C:AASC2
C:aade3
D:1
D:199

Here is my c program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>

int main(int argc, char **argv)
{

    char buf[BUFSIZ];
    short a = 0;
    short b = 0;
    short anzb=0;
    short anza=0;
    char *c
    short *d;

    short i;


    while (fgets(buf, BUFSIZ, stdin) != NULL)
    {
        if (buf[strlen(buf)-1] == '\n') {
            char *isa = strstr(buf, "A:");
            char *isb = strstr(buf, "B:");
            char *isc = strstr(buf, "C:");
            char *isd = strstr(buf, "D:");
            if(isa){
                char *sep = substring(isa,3,strlen(isa));
                a = atoi(sep);
                d = malloc(a * sizeof(short));
            }else if(isb){
                char *sep = substring(isb,3,strlen(isb));
                b = atoi(sep);
                c = malloc(b * sizeof(char));
            }else if(isc){
                char *sep = substring(isc,3,strlen(isc));
                c[anzc] = sep;
                anzc++;
            }else if(isd){
                char *sep = substring(isd,3,strlen(isd));
                d[anzd] = sep;
                anzd++;
            }
        }
    }

    printf("%i\n", a);
    printf("%i\n", b);

    for(i=0; i<=anzc-1;i++){
        printf("%c", c[i]);
    }

    return 0;
}

I am new in c so i dont't have much knowledge about pointers and arrays so i hope you could help me.

After the values A: and B: are read and stored in a and b i could create my array for the lines of c and d. And i think here is my problem. I dont know how i could create an array at this time in my program. I tried with malloc and other things but there is my knowledge zu small for.

I only want to create an array at the time if i have read the values (sizes) for c and d (A and B).

And then i want to save the values in the array.

I hope you could help me to fix my code. I have tried much at this day but nothing worked and i am now very helpless.

EDIT:

New try where i get an segmentation fault 11:

     else if(isb){
        char *sep = substring(isb,8,strlen(isb));
        b = atoi(sep);
        c = malloc(b * sizeof(char*));
        int i;
        for (i = 0; i < subst; i++)
        {
          c[i] = malloc(13);
        }
    }else if(isc){
        char *sep = substring(isc,8,strlen(isc));
        strcpy(c[anzc], &buf[3]);
        anzc++;
    }

1 Answer 1

1

Your allocation is more or less correct however you're overlooking some details. B provides you a value of 3, that is how many entries there are for C, not the length of each. You then allocated a single array when you in fact need a 2-D array which should be of type char* this array will point to 3 other arrays which will contain each of the values on the C lines. So;

This line c = malloc(b * sizeof(char)); needs to be c = malloc(b * sizeof(char*)); Then you need to do;

 int i;
 for (i = 0; i < b; i++)
 {
     c[i] = malloc(length); // where length is some arbitrary buffer length
     // because you have no way of knowing the length of the individual strings.
 }

After this you can use strcpy to copy the lines into each of the char arrays you've allocated in the for loop.

So to accomplish the copy you need to do something like this;

         int iC = 0;
         // outside of the while loop we need a control var track the index
         // of the c array. it needs to work independent of the normal iteration.
         //inside the while loop
         else if(isc){
            strcpy(c[iC], &buf[3]) 
            iC++;
        }
Sign up to request clarification or add additional context in comments.

7 Comments

maybe you could tell about the strlen(). he uses strlen()-1
need i copy the data? i am only resizing my array one time. at the time if i know how much entries there are.
@bladepit imagine the array as a table, like in excel. That code only creates the table. The data, ie x where x is C:x is not being copied into it. You need to use strcpy to accomplish that. The buffer should be set generously so that you don't get overflows. Or you can use one of the safe string copy methods that specify the length of the data.
i am so confused. with char* c i create an 2d array? i want to save in my array [0] = AAAA1; [1] = AASC2 and so on. The length of the entries is always the same. only in d is it dynamic. at the beginning c is empty then i read b from the stdin and then i create c. after that if i read c from stdin i put the string in the array. that are my needs and i am so confused that i don't know what i should do now...:-(
@bladepit yes you're correct about the 2d array. After it's allocated you need to copy each of the C values into indexes 0-2. I'll update with some logic to guide you on that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.