Command line arguments and read file/print text in

2019-07-31 12:53发布

I am trying to work with command line arguments and parsing a text file in C. Basically I want to be able to put in two numbers, like, 1 and 4 and have it read a column of a text file then print it to stdout. I want to be able to do something like take this:

PID   TTY        TIME     CMD
449   ttys000    0:00.35 -bash
1129  ttys001    0:00.35 -bash
25605 ttys001    0:00.15  vi prog.c
6132  ttys002    0:00.11 -bash
6208  ttys002    0:00.03  vi test

And do:

./your_prog 1 4 < data.txt 

PID CMD
449 bash
1129 -bash
25605 vi
6132 -bash 
6208 vi 

So I need to enter the the columns i want to print out, redirect the file in "data.txt" and have it process the file and print like so.

So far I have this for my code:

 #include <stdio.h>
 int main(int argc, char **argv){
//int row = argc[0];
//int col = argc[1];
//if number entered is less than one, re-enter

int i;
for (i = 0; i < argc; i++) {
  printf("argv[%d] = %s\n", i, argv[i]);
}
if(argc < 1){
  fprintf(stderr, "Enter a valid input");

  //quit
  return 1;
} 
else{   
  char ch[256];
  //ch[255] = '\0';

    while(fgets(ch, 256, stdin) != NULL){
      printf("%s", ch);
    }
  } 
  return 1;
}

but am not sure if I am on the correct track and am confused as to what to do next. I am new to C, so I apologize if this is an easy question.

2条回答
别忘想泡老子
2楼-- · 2019-07-31 13:13

Here is what I have so far. I can get it to parse apart then I can put it back how it was, but for the life of me I cant figure out how to tell it to print only the first column. If I could have some direction, then I could figure out how to do the rest, but I can find anything. Here is what code I have so far:

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


int main(int argc, char **argv){
int col1, col2;
int size = 512;
char ch[size];
char *temp[size];

char *token;

if(argc == 1){
fprintf(stderr, "I need more!\n");
return 1;
}

else{
//test to see what is stored

  int i;
  for (i = 0; i < argc; i++) {
    printf("argv[%d] = %s\n", i, argv[i]);   
  }

  if(sscanf(argv[1], "%d", &col1) != 1) return 1;
  if(sscanf(argv[2], "%d", &col2) != 1) return 1;   

  while(fgets(ch, size, stdin) != NULL){
    //get 1st token
    token = strtok(ch, " ");
      while(token != NULL){
        printf(" %s", token);
        temp[i++] = token;
        token = strtok(NULL, " ");
      }   
    } 
    return 0;   
  }
}
查看更多
Juvenile、少年°
3楼-- · 2019-07-31 13:15

To get you started, this improves the way you gather the command line information. Note the first argument argv[0] is the name of the executable itself. Bear in mind comments above, I haven't improved any further. Syntax is progname filename col1 col2

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

void fatal(char *msg) {
    printf("%s\n", msg);
    exit (1);
}

int main(int argc, char*argv[])
{
    int col1, col2;
    FILE *fp;
    char ch[256];

    if(argc < 4)
        fatal("Need three arguments");

    if (sscanf(argv[2], "%d", &col1) != 1)
        fatal("Argument 1 error");

    if (sscanf(argv[3], "%d", &col2) != 1)
        fatal("Argument 2 error");

    printf("Read columns %d and %d from file %s\n", col1, col2, argv[1]);
    if ((fp = fopen(argv[1], "r")) == NULL)
        fatal("Unable to open file");

    // process the file
    while (fgets(ch, 256, fp) != NULL) {
        printf("%s", ch);
    }
    fclose(fp);
    return 0;
}
查看更多
登录 后发表回答