Hi,
I'm new to C programming. I have a question and I'm stuck for weeks. I have a file (example: abc.txt) with the following values, tab delimited:
And I want only the selected column 4 with the number 2 data to pull out to a new file lets say def.txt, and the data will look like this:a1 \t b4 \t c5 \t 1
a5 \t b1 \t c3 \t 1
a6 \t b2 \t c4 \t 1
a2 \t b3 \t c7 \t 2
a1 \t b4 \t c5 \t 2
a5 \t b1 \t c3 \t 2
a6 \t b2 \t c4 \t 3
a2 \t b3 \t c7 \t 3
a2 \t b3 \t c7 \t 3
How can I achieve doing that? I'm using C, my head is like gonna explode looking at it. Here's my code:a2 \t 2
a1 \t 2
a5 \t 2
I haven't write the write on new file part, still stuck on sorting out the data part. Need advice and thank you in advance!Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { static const char filename[] = "abc.txt"; FILE *file = fopen(filename, "r"); if ( file != NULL ) { char line[BUFSIZ]; char col1[BUFSIZ], col2[BUFSIZ], col3[BUFSIZE]; int col4[BUFSIZ]; while ( fgets(line, sizeof line, file) != NULL ) { if ( sscanf(line, "%s %s %s %s", col1, col2, col3, &col4) == 4 ) { if (col4 == 2) { printf("%s\t%s\n", col1, col4); } } } system("pause"); } else { perror(filename); } return 0; }