C Turning lines of an archive into arrays

前提是你 提交于 2019-12-13 00:24:02

问题


I have an archive and I want to turn every line into an array: v[i].data. However, when I run the code it shows zeros for the arrays. Is there anything I should change?

Input

1760
02/20/18,11403.7
02/19/18,11225.3
02/18/18,10551.8
02/17/18,11112.7
02/16/18,10233.9

Actual Output

1761
0

Expected Output

1761
02/20/18,11403.7

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


typedef struct{
    char data[20];

}vetor;

int main(int argc,char *argv[]){
    FILE *csv;

        if((csv=fopen(argv[1], "r")) == NULL  )
        {
            printf("not found csv\n");
            exit(1);
        }


        long int a=0;

        char linha[256];

        char *token = NULL;

        if(fgets(linha, sizeof(linha), csv)) //counting lines
        {
            token = strtok(linha, "\n");
            a =(1 + atoi(token));
        }


        printf("%d\n", a);

        rewind(csv);

        vetor *v;

        v=(vetor*)malloc(a*sizeof(vetor));

        char linha2[256];

        while (fgets(linha2, sizeof(linha2), csv) != 0)
        {
            fseek(csv, +1, SEEK_CUR);

            for(int i=0;i<a;i++)
            {   
                fscanf(csv, "%[^\n]", v[i].data);


            }
        }

        printf("%s\n", v[0].data);


    fclose(csv);


    return 0;
}

回答1:


There were a number of mistakes so I went ahead and rewrote the problem areas with comments explaining what I did

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

typedef struct{
    char data[20];

}vetor;

int main(int argc,char *argv[]){
    FILE *csv;

    if((csv=fopen(argv[1], "r")) == NULL  )
    {
        printf("not found csv\n");
        exit(1);
    }

    char line[20];

    // Read number of lines
    int num_lines = 0;
    if (!fgets(line, sizeof(line), csv)) {
        printf("Cannot read line\n");
        exit(1);    
    }
    char* token = strtok(line, "\n");
    num_lines = atoi(token) + 1;
    vetor* v = malloc(num_lines * sizeof(vetor));

    // Fill in vetor
    int i = 0;
    while (fgets(line, sizeof(line), csv) != NULL) {
        int len = strlen(line);
        line[len-1] = '\0'; // replace newline with string terminator
        strcpy(v[i].data, line); //copy line into v[i].data
        i++;
    }

    printf("%d\n", num_lines);
    for (i = 0; i < num_lines; i++) {
            printf("%s\n", v[i].data);
    }

    return 0;
}

I think the main mistake was a misunderstanding of how best to read in each line of information. If I understood correctly you want each 02/20/18,11403.7 line to be an element in the vetor array.

The easiest way is to simply get each line one at a time with fgets

while (fgets(line, sizeof(line), csv) != NULL) 

Change the ending character from newline to the string terminating character '\0'

int len = strlen(line);
line[len-1] = '\0';

Then copy the string into the ith element of vetor and update i for the next iteration of the loop.

strcpy(v[i].data, line);
i++;


来源:https://stackoverflow.com/questions/49395313/c-turning-lines-of-an-archive-into-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!