Solved

using getopt to read till EOF

Posted on 2009-02-13
16
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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