Link to home
Start Free TrialLog in
Avatar of kuntilanak
kuntilanakFlag for United States of America

asked on

using getopt to read till EOF

okay, so I am currently using the command getopt to get options from std in and one of the option I want is a -j

what I want is if say -j isn't specified by the user then I want it to read from stdin until EOF

how can I do this?

Also how can I do a check so that I know -j is specified or not?
Avatar of jkr
jkr
Flag of Germany image

You can check for 'j' while 'getopt()' is evaluating the arguments, e.g. like
int
main(int argc, char *argv[ ])
{
    extern char *optarg;
    extern int optind, optopt;
 
    bool j_present = false;
 
    while ((c = getopt(argc, argv, "j")) != -1) {
        switch(c) {
        case 'j':
            j_present = true;
            break;
        }
 
    if (!j_present) {
 
      char buf[256];
 
      while(!feof(stdin)) {  // read from stdin till EOF
 
        fgets(buf,sizeof(buf),stdin);
      }
    }
 
    //...
 
    return 0;
}

Open in new window

Oh, and the same with C++ streams:
#include <iostream>
#include <string>
using namespace std;
 
int
main(int argc, char *argv[ ])
{
    extern char *optarg;
    extern int optind, optopt;
 
    bool j_present = false;
 
    while ((c = getopt(argc, argv, "j")) != -1) {
        switch(c) {
        case 'j':
            j_present = true;
            break;
        }
 
    if (!j_present) {
 
      string buf;
 
      while(!cin.eof()) {  // read from stdin till EOF
 
        getline(cin,buf);
      }
    }

Open in new window

Avatar of kuntilanak

ASKER

how do you input an EOF in C?
I think what my question asks is this, if it's just sitting there in the while loop waiting for an EOF from stdin/user, what should the user type in in order for it to go out of that loop?
EOF is defined in stdio.h as

#define EOF     (-1)

so you can just use it that way, e.g.
printf("This text ends with EOF%c", EOF);
 
// or
 
cout << "This text ends with EOF" << EOF;

Open in new window

Hmm... not sure if that's what I am looking for, when the program enters the while loop below. It just waits there for something to get out of it right? It's waiting for the user to type something, an EOF so it can go out. So to be more specific and a bit dumb. What should I press in my keyboard to get it out of that loop?
  while(!feof(stdin)) {  // read from stdin till EOF
 
        fgets(buf,sizeof(buf),stdin);
      }

Open in new window

I think we should check if buf has EOF
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm..this is kind of an off topic question but somehow related.. say after the -j option it should only take an int, so for example -j 123 , how do I check  if it is an integer and not a char
That you can do like


#include <stdlib.h>
 
bool get_num(char* p,int& num) {
 
  char* pcEnd = NULL,
 
  num = (int) strtol(p,&pcEnd,10);
 
  if (*pcEnd == '\0') return true; // valid number
 
  return false;
}
 
//...
 
    while ((c = getopt(argc, argv, "j")) != -1) {
        switch(c) {
        case 'j':
 
            char* value = (char*) optarg;
            int num;
            j_present = get_num(value,num);
            break;
        }

Open in new window


 EOF is ctrl-z
hmmm... that's basically for converting a char to an int.. what I want is that if they do something like
-j abasd then I want to print out an error message as the correct way to supply to the option would be -j 1234
Well, if that was '-j abasd' the flag would not be set and they would be asked, but  you could make that
    while ((c = getopt(argc, argv, "j")) != -1) {
        switch(c) {
        case 'j':
 
            char* value = (char*) optarg;
            int num;
 
            if(!get_num(value,num)) {
 
              printf("Error: Needs to be '-j <numeric value>'\n");
            }
            else j_present = true;
 
            break;
        }

Open in new window

isn't there a simpler approach to this? will get_num store the int value into num?
Yes, it will store the actual value read from '-j' there. And I am afraid it won't get much simpler, since you have to "manually" validate each argument anyway, since "getopt()" cannot do that.
okay.. thanks jkr!