helps store csv value in a EM C vector

Asked

Viewed 40 times

0

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




int process_field2(int fcount, char *value) {
   
    int vetor[36];
    
    if (fcount == 0) {
    for (int k=0; k<36; k++){
        vetor[k] = atoi(value);}

    }
    printf("%d", vetor[1]);
 
    
}



int testes2(){
    
    char buf[1024];
    char token[1024];
    int row_count = 0;
    int fcount = 0;
    int in_double_quotes = 0;
    int token_pos = 0;
    int i = 0;
    
    FILE *fp = fopen("posgraduacaoresidencia.csv", "r");

    if (!fp) {
        printf("Can't open file\n");
        return 0;
    }


    while (fgets(buf, 1024, fp)) {
        row_count++;

        if (row_count == 1) {
            continue;
        }

        fcount = 0;
        i = 0;
        do {
            token[token_pos++] = buf[i];

            if (buf[i] == ';' ) {
                token[token_pos - 1] = 0;
                token_pos = 0;
                process_field2(fcount++, token);
                break;
        
}
        } while (buf[++i]);
    
        printf("\n");
        
    }
}







int main(void) {
  
    testes2();      
    return 0;
}

However this storing all the values of the column in each of the positions of the vector, I would like to store each data of the row in 1 vector position...

inserir a descrição da imagem aqui

  • It seems to me that you are doing exactly what you have programmed to do. Give an example of an entry and what you want as a result. The function process_field2 it seems to me meaningless. Note also that vetor is a local variable and therefore its content is lost at the end of the execution of the function.

  • wish as a result that each value in the photo enters a position of the vector, however it is entering all values in each position of the vector, instead of 1 value in each position

  • But you pass only one value to your function! You assign this value, converted to integer, to all vector positions. In addition to each execution of the function it loses what was stored in the other executions. Rethink your logic.

  • could give me a light, to lost kk

  • I’m very beginner still, starting now and so, but I’m having a hard time getting a solution, for days already trying, I need to get this data in a vector to organize them in ascending order

  • What is the contents of the file ?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.