Solved

using getopt to read till EOF

Posted on 2009-02-13
16
509 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
ID: 23638048
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
ID: 23638095
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
ID: 23638234
how do you input an EOF in C?
0
 

Author Comment

by:kuntilanak
ID: 23638252
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
ID: 23638279
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
ID: 23638293
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
ID: 23638310
I think we should check if buf has EOF
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 23638317
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kuntilanak
ID: 23638341
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
ID: 23638371
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
ID: 23638373

 EOF is ctrl-z
0
 

Author Comment

by:kuntilanak
ID: 23638393
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
ID: 23638415
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
ID: 23638505
isn't there a simpler approach to this? will get_num store the int value into num?
0
 
LVL 86

Expert Comment

by:jkr
ID: 23638513
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
ID: 23638515
okay.. thanks jkr!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a complete list of ASP.NET page load events 3 29
Export import database 4 45
Replace &lt; with < 14 57
free scanner TWAIN can be operated with a Web application 9 41
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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 the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 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

21 Experts available now in Live!

Get 1:1 Help Now