ecjb
ecjb

Reputation: 5459

How to set the pointer back to the beginning of the file while parsing a csv file with the strtok()

I have a .csv file that I want to parse as a database with rows and column numbers The solution I found uses strtok() (which might be not the perfect solution but I am happy to hear other suggestions). To my understanding strtok() progressively moves the pointer within the file. If you want to parse the file again, for example to access another cell, I have to close and reopen the file to set the pointer back to the beginning of the file. Is it possible to set the pointer at the beginning of the file (without having to close and reopen it)?

/* database.c */
#include <stdlib.h>
#include "main.h"

#define BUFFLEN 1024

void print_entire_file(FILE *fp) {
    char buffer[BUFFLEN];
    int row = 0;
    int column = 0;

    while (fgets(buffer, BUFFLEN, fp)) {
        column = 0;
        row++;

        char *value = strtok(buffer, &csv_separator);

        while (value) {
            if (column == 0) {
                printf("\t");
            }

            if (column == 1) {
                printf("\t");
            }

            if (column == 2) {
                printf("\t");
            }

            printf("%s", value);
            value = strtok(NULL, &csv_separator);
            column++;
        }
    }
}

struct s_df_dim get_file_height(FILE *fp) {
    struct s_df_dim df_dim;
  
    char buffer[BUFFLEN];
    int row = 0;
    int column = 0;
    while (fgets(buffer, BUFFLEN, fp)) {
        column = 0;
        row++;
        if (row == 1)
            continue;
        char *value = strtok(buffer, &csv_separator);
        while (value) {
            value = strtok(NULL, &csv_separator);
            column++;
        }
    }
    df_dim.nrow = row;
    df_dim.ncol = column;

    fclose(fp);
    return df_dim;
}

void print_cell(FILE *fp, int colid, int rowid) {
    char buffer[BUFFLEN];
    int row = 0;
    int column = 0;

    while (fgets(buffer, BUFFLEN, fp)) {
        column = 0;
        row++;

        char *value = strtok(buffer, &csv_separator);

        while (value) {
            if ((row == rowid) && (column == colid)) {
              printf("%s", value);
            }
            value = strtok(NULL, &csv_separator);
            column++;
        }
    }
}
/* main.h */

char csv_separator = ';'; // choose between ',' and ';'

struct s_df_dim {
    int nrow;
    int ncol;
};
/* functions.h */

#include <stdlib.h>
#include "main.h"

#define BUFFLEN 1024

void print_entire_file(FILE *fp) {
    char buffer[BUFFLEN];
    int row = 0;
    int column = 0;

    while (fgets(buffer, BUFFLEN, fp)) {
        column = 0;
        row++;

        char *value = strtok(buffer, &csv_separator);

        while (value) {
            if (column == 0) {
                printf("\t");
            }

            if (column == 1) {
                printf("\t");
            }

            if (column == 2) {
                printf("\t");
            }

            printf("%s", value);
            value = strtok(NULL, &csv_separator);
            column++;
        }
    }
}

struct s_df_dim get_file_height(FILE *fp) {

    struct s_df_dim df_dim;
  
    char buffer[BUFFLEN];
    int row = 0;
    int column = 0;
    while (fgets(buffer, BUFFLEN, fp)) {
        column = 0;
        row++;
        if (row == 1)
            continue;
        char *value = strtok(buffer, &csv_separator);
        while (value) {
            value = strtok(NULL, &csv_separator);
            column++;
        }
    }
    df_dim.nrow = row;
    df_dim.ncol = column;

    fclose(fp);
    return df_dim;
}

void print_cell(FILE *fp, int colid, int rowid) {
    char buffer[BUFFLEN];
    int row = 0;
    int column = 0;

    while (fgets(buffer, BUFFLEN, fp)) {
        column = 0;
        row++;

        char *value = strtok(buffer, &csv_separator);

        while (value) {
            if ((row == rowid) && (column == colid)) {
              printf("%s", value);
            }
            value = strtok(NULL, &csv_separator);
            column++;
        }
    }
}
#makefile

CC = gcc 
CFLAGS = -g -Wall

BINC=database

allc: $(BINC) 

$(BINC): $(BINC).c *.h
    $(CC) $(CFLAGS) $< -o $@

clean:
    $(RM) -rf $(BINC) *.dSYM

runc: allc
    ./$(BINC)

and file.csv:

asdf;8234;a
sdfasg;3479;z
rasdb;10984;d
adwekrj;23460;c

Upvotes: 0

Views: 30

Answers (0)

Related Questions