从csv文件读入2d数组并使用c对数组进行排序

时间:2017-03-30 17:17:32

标签: c arrays csv

在我的csv文件中,目前我有像

这样的数据
45454, 32, 78, C, 67
45452, 22, 38, C, 34

我想将数据添加到2d数组中并对它们进行排序。 目前我正在使用这种方法

#include<stdio.h>
#include<conio.h>
#include<String.h>

void getData(char *buff);
int main()
{
    FILE *fp = fopen("file1.csv", "r");
    int count = -1;
    do
    {
        char buff[1024];
        fgets(buff, 1024, (FILE*)fp);
        count++;
        if(count != 1)
        {
            printf(buff);
            getData(buff);
        }
    }
    while((getc(fp))!= EOF);


}
void getData(char buff[])
{
    char *token = strtok(buff,",");
    int counter=0;

    while( token != NULL )
    {
        counter++;
        printf(" %s\n",token);
        token = strtok(NULL,",");
    }
}

但是我想用fgets和sscanf读取数据并将它们存储到单维VLA(可变长度数组)中 我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您实际上并不需要二维数组。你只需要一个包含线条的数组。你走了:

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

struct row {
    int id;
    int col1;
    int col2;
    char col3;
    int col4;
};

int cmp(const void *a1, const void *a2) {
    const struct row *r1 = a1;
    const struct row *r2 = a2;
    return r1->id - r2->id;
}

void line_to_row(char *buff, struct row *r)
{
    char *saveptr;
    char *token = strtok_r(buff, ",", &saveptr);
    if (token == NULL) {
        exit(1);
    }
    r->id = atoi(token);

    token = strtok_r(buff, ",", &saveptr);
    if (token == NULL) {
        exit(1);
    }
    r->col1 = atoi(token);

    token = strtok_r(buff, ",", &saveptr);
    if (token == NULL) {
        exit(1);
    }
    r->col2 = atoi(token);

    token = strtok_r(buff, ",", &saveptr);
    if (token == NULL) {
        exit(1);
    }

    while (*token != '\0') {
        r->col3 = *token;
        token++;
    }

    r->col4 = atoi(buff + 1);
}

int main()
{
    FILE *fp = fopen("file1.csv", "r");
    if (fp == NULL) {
        exit(1);
    }
    char buff[1024 + 1];
    int c, i = 0, len = 10;
    struct row *rows = malloc(sizeof(*rows) * len);
    struct row *temp = rows;

    while (fgets(buff, 1024, (FILE*)fp)) {
        if (i == len - 1) {
            len += 10;
            temp = realloc(rows, sizeof(*rows) * len);
            if (temp == NULL) {
                exit(1);
            }
            rows = temp;
        }
        line_to_row(buff, &rows[i]);
        i++;
    }
    fclose(fp);

    if (i == 0) {
        return 0;
    }

    len = i;
    temp = realloc(rows, sizeof(*rows) * len);

    if (temp == NULL) {
        exit(1);
    }

    rows = temp;

    qsort(rows, len, sizeof(*rows), &cmp);

    for (i = 0; i < len; i++) {
        printf("%d\n", rows[i].id);
    }
    free(rows);
    return 0;
}

答案 1 :(得分:0)

这是我的工作代码

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

typedef struct
{
    int id, exam1, exam2, exam3;
    char grade;
} Row;

int getSize(char* filename)
{
    int rows=0;
    FILE* file = fopen(filename, "r");
    char line[1024];
    while (fgets(line, 1024, file) != NULL)
    {
        rows++;
    }

    return rows;
}

void sort(Row *a, int length)
{
    int j;
    Row temp;
    int i;
    for (i = 1; i < length; i++)
    {
        j = i;
        while (j > 0 && a[j].id < a[j - 1].id || (a[j].id == a[j - 1].id && a[j].grade < a[j - 1].grade))
        {
            temp = a[j];
            a[j] = a[j - 1];
            a[j - 1] = temp;
            j--;
        }
    }
}

int binarySearch(Row *row, int size, int key)
{
    int i, start, end, mid;
    start = 0;
    end = size;

    do
    {
        mid = (int)((start + end)/2);
        if(row[mid].id == key)
        {
            printf("\n");
            printf("+--------+");
            printf("-----------+");
            printf("-------+\n");
            printf("| Index  |  Student  | Grade |\n");
            printf("+--------+");
            printf("-----------+");
            printf("-------+\n");
            printf("| %-5d  |  %-5d   |\t %-4c|\n", mid+1, row[mid].id, row[mid].grade);
            printf("+--------+");
            printf("-----------+");
            printf("-------+\n");
            printf("\n");
            break;
        }
        else if(row[mid].id > key)
        {
            end = mid - 1;
        }
        else
        {
            start = mid + 1;
        }
    }
    while(start <= end);

    if(start > end)
    {
        printf("\n");
        printf("+--------+");
        printf("-----------+");
        printf("-------+\n");
        printf("| Index  |  Student  | Grade |\n");
        printf("+--------+");
        printf("-----------+");
        printf("-------+\n");
        printf("| N/A   |  N/A    |\t N/A|\n");
        printf("+--------+");
        printf("-----------+");
        printf("-------+\n");
        printf("\n");
    }
}

int main()
{
    char* filename = "grades.csv";
    int rows = getSize(filename);
    printf("There are %d lines in file grades.csv.\n\n", rows);
    FILE* stream = fopen("grades.csv", "r");
    Row *row = malloc(sizeof(*row) * rows);
    char line[1024];
    int i = 0;

    printf("Original:\n");
    printf("+--------+");
    printf("-----------+");
    printf("-------+");
    printf("------+");
    printf("-------+\n");
    printf("| Student|    Exam 1 | Exam 2|Exam 3| Grade |\n");
    while(fgets(line, 1024, stream) != NULL)
    {
        printf("+--------+");
        printf("-----------+");
        printf("-------+");
        printf("------+");
        printf("-------+\n");
        sscanf(line, "%d,%d,%d,%d,%c", &row[i].id, &row[i].exam1, &row[i].exam2, &row[i].exam3, &row[i].grade);
        printf("| %d |\t %-4d|\t %-4d|\t%-4d|\t%-4c|\n", row[i].id, row[i].exam1, row[i].exam2, row[i].exam3, row[i].grade);
        i++;
    }
    printf("+--------+");
    printf("-----------+");
    printf("-------+");
    printf("------+");
    printf("-------+\n");
    printf("\n\n");


    printf("Sorted:\n");
    sort(row, rows);
    printf("+--------+");
    printf("-----------+");
    printf("-------+\n");
    printf("| Index  |  Student  | Grade |\n");
    for(i = 0; i < rows; i++)
    {
        printf("+--------+");
        printf("-----------+");
        printf("-------+\n");
        printf("| %-5d  |  %-5d   |\t %-4c|\n", i+1, row[i].id, row[i].grade);
    }
    printf("+--------+");
    printf("-----------+");
    printf("-------+\n");

    int sid;
    int ex = 0;

    while(1)
    {
        printf("\n\nEnter student ID (-1 to quit): ");
start:
        scanf("%d", &sid);

        if(sid == -1)
        {
            return 0;
        }
        else if(sid > 100000 && sid < 999999)
        {
            binarySearch(row, rows, sid);
            printf("\n\nEnter student ID (-1 to quit): ");
            goto start;
        }
        else
        {
            printf("\nERROR and enter again ([100000,9999999] and -1 to quit): ");
            goto start;
        }
    }

    fclose(stream);
    free(row);

    return 0;
}