IMPLEMENTATION OF CUT -F COMMAND IN C

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

void main(int argc,char *argv[])
{
    char line[100];
    char *token;
    int count=0;
    FILE *fp;
    fp = fopen(argv[2],"r");

    while ( fscanf(fp,"%[^\n]\n",line ) != EOF )
         {
        count = 0;
        token = strtok(line," ");
        while(token != NULL)
        {
            count++;
            if(count == atoi(argv[1]))
            {
                printf("%s\n",token);
                token = strtok(NULL," ");
                break;
            }
            else
            {
                token = strtok(NULL," ");
                continue;
            }
           
        }

    }
    fclose(fp);
}


OUTPUT :

cc cut-f.c
./a.out feild filename

Comments