I want to be able to ask the user which column he wants to print from the input file. The input file is a standard text file that has 4 columns (as described by the code in the global variables) that are separated by space (which is what I'm using to separate each line in the getline function.)
Thank you.
#include <stdio.h>
#include <string.h>
#define LINE_SIZE 256
#define COL_NUM 3
int main()
{
char line[LINE_SIZE];
char *ptr;
int column;
FILE * fp = fopen("input.txt", "r");
while(!feof(fp)){ // if not the end of file
fgets(line, sizeof(line), fp); // get one line each time
ptr = strtok(line, " "); // split line by space
column = 1; // starting column is one
while(ptr != NULL) // if the line is not finished
{
if(column == COL_NUM){
printf("%s\n", ptr); // print what we got
ptr = strtok(NULL, " "); // and keep splitting
}
ptr = strtok(NULL, " "); // and keep splitting
column++;
}
}
fclose(fp);
return 0;
}