0

(C LANGUAGE)I have the following data in a text file and want to filter the people with specific age and who earns more than x salary and are below y age.

ASSUMING the first column being age and last being salary e.g.

39, State-gov, 77516, Bachelors, 13, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K
50, Self-emp-not-inc, 83311, Bachelors, 13, Married-civ-spouse, Exec-managerial, Husband, White, Male, 0, 0, 13, United-States, <=50K
  • Why do you need to do this in C. Almost every other language will be easier, especially some of the Unix text processing languages. If you have to do it in C then use a regex library. – ctrl-alt-delor Jan 25 '21 at 06:50

1 Answers1

0

It seems that the input file is actually CSV format. I would suggest to refer to Read .csv file in C

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

const char* getfield(char* line, int num)
{
    const char* tok;
    for (tok = strtok(line, ",");
            tok && *tok;
            tok = strtok(NULL, ";\n"))
    {
        if (!--num)
            return tok;
    }
    return NULL;
}

int main()
{
    FILE* stream = fopen("input", "r");

    char line[1024];
    while (fgets(line, 1024, stream))
    {
        char* tmp = strdup(line);
        printf("Field 3 would be %s\n", getfield(tmp, 3));
        // NOTE strtok clobbers tmp
        free(tmp);
    }
}

You could also looks for CSV parser libraries such as: https://github.com/semitrivial/csv_parser

Robert
  • 2,711
  • 7
  • 15