?
Solved

using getopt to read till EOF

Posted on 2009-02-13
16
Medium Priority
?
529 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
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…
Suggested Courses

807 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