How to read a line in C

2

How to read a row of integers and store in a dynamic-sized vector?

I currently read the string as a string (though the input is only integer) using gets, the only way that worked.

The code looks like this:

    type def struct{char entradas[50];} Processo 

    int main(){[...] Processo processos; gets(processo[i].entradas); }

And it works, but I need to be variable this size of entries []

    
asked by anonymous 12.08.2014 / 19:43

1 answer

2

You might as well continue with what you are doing and create a vector of virtually unlimited size, eg% w / w

Or create a buffer and go load the items partially:

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

void* vetgen(void *vetor,int newsize,int size){
    void *newarr=malloc(newsize);
    memcpy(newarr, vetor,size);
    free(vetor);
    return newarr;
}
int main(){
    int* vetor,i,aux=1;
    vetor=malloc(sizeof(int)*256);
    do{
        scanf("%d ",vetor+i);
        i++;
        if(i%256==0){
            vetor = (int*) vetgen(vetor,i*(aux+1),i*aux);
            aux++;
        }
    }while(!feof(stdin));
}

This code is just to give you an idea, to read entries in C even from entrada[500000]; recommend using stdin and fgets()

    
12.08.2014 / 20:59