Solved

using getopt to read till EOF

Posted on 2009-02-13
16
508 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:kuntilanak
  • 8
  • 7
16 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

0
 

Author Comment

by:kuntilanak
Comment Utility
how do you input an EOF in C?
0
 

Author Comment

by:kuntilanak
Comment Utility
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?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

0
 

Author Comment

by:kuntilanak
Comment Utility
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

0
 

Author Comment

by:kuntilanak
Comment Utility
I think we should check if buf has EOF
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Um, this would be a bit different. "Reading from stdin until EOF" only makes sense if that input is provided by anothre program. If you want to get user input, don't wait for EAO, but just have them enter a value, e.g.
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];

 

      printf("Please enter the 'j' value:\n");

      fgets(buf,sizeof(buf),stdin);

      

    }

 

    //...

 

    return 0;

}
 

// or
 

#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;

 

      cout << "Please enter the 'j' value" << endl;

      getline(cin,buf);

      

    }

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:kuntilanak
Comment Utility
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
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

0
 
LVL 12

Expert Comment

by:williamcampbell
Comment Utility

 EOF is ctrl-z
0
 

Author Comment

by:kuntilanak
Comment Utility
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
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

0
 

Author Comment

by:kuntilanak
Comment Utility
isn't there a simpler approach to this? will get_num store the int value into num?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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.
0
 

Author Comment

by:kuntilanak
Comment Utility
okay.. thanks jkr!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now