command line

Pages: 12
I dont quite know where to start??

Last edited on
"Simple variant of X" What is that X?

The task implies that you have studied the signature of main() that takes parameters. Have you? What those parameters are?
where to start?

write a program that can print out the first n lines of a text file. Can you do that yet?
write a main() function that can get the user parameters from the command line, Can you do that yet?

When you can do those two things, your overall task will appear simple.
Yea,
Last edited on
Last edited on
Lets loan from your other thread:
void read_numbers( ifstream& f, vector<double>& numbers );
Not a verbatim loan, but part of the idea:
void show_head( std::ifstream& f, size_t count );

What should it do? It gets an open stream and a number.
It should read at most count lines and print them.

Could you write the implementation for such function?
hint: When reading text files you can count the number of lines in them by counting carriage returns.

For the command line processing, use getopt() if your environment supports it.
So is the string_to_integer function all set?

Last edited on
It's a good idea to use argv[0] for the program name in the usage function. That way the user can rename the program and it will still make sense:
[code]int main(int argc, char* argv[])
{
usage(argv[0]);
return 0;
}


void usage(const char *argv0)
{
cout << "usage: " << argv0 << " [-n #] file1 [file2] ..." << endl;
}[/code

Does your system have getopt()? If so then use it for the command line processing. search the web for some examples.
is getopt() the same as getline function? I never was told about getopt()
No, it is not. The getopt() is not part of C++ standard. You should be able to find its description from the net.

The Boost libraries do have C++ code for managing options, i.e. an alternative for getopt(), but Boost has to be installed separately. Some bits of Boost have already been copied into the C++ standard.


However, you are doing homework and I bet that the purpose of this one is to learn about argc and argv and more importantly to practice creating a logical set of tests that cover all the possibilities.
Okay, so I guess I need a for loop or something in main. I really just have no idea what to do.....

I'd appreciate some hints like have for loop in main, or put this in usage() etc...
Thanks
Do you have getopt()? You'd use it sort of like this (untested). Note that this assumes usage() exits the program after printing the message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int opt;
unsigned numLines = 10;  // number of lines to print. 10 is default
while ((opt = getopt(argc, argv, "n:") != EOF) {
    switch (opt) {
    case 'n':
        numLines = atoi(optarg);
        if (numLines < 1) {
            usage(argv[0]); // bad argument to -n
            // You need to add more error checking here for things like "-n42bleh"
        }
        break;
    default:
        usage(argv[0]); // unknown option
    }
}

// optind is now the index of file argument
if (optind >= argc) usage(argv[0]);   // no file given
for (unsigned i=optind; i< argc; ++i) {
    head(argv[i]);
}
return 0;


What if you have a file named "-myfile"? How can the program tell that from a bad command line argument. Worse still, what if you have a file named "-n33"?? How can it know to process that as a file and not as "-n 33"?? What if you want to print the first 88 lines of a file called -n88???

Getopt actually handles all of these cases. You can force the option processing to stop by entering "--":
getopt -n88 -- -n88 // Print the first 88 lines of file "-n88"
This is why you really want to use getopt().
I dont know why everyone is saying use getopt(). I dont think I can add any functions to what I was given in the starter code.
Thanks
You can probably call available functions, and hopefully getopt() is one of them. In the unix world, getopt() is used pretty universally for command line parsing, which makes command interfaces very consistent.

If you can't use getopt() then you'll have to write your own code to parse the command line. The various things to check for are:
- no option after "-n"
- invalid option after "-n", including something that starts with digits, or a negative number
- invalid option (e.g., -a)
- No file specified.
Got alot closer. If I enter f1.txt or f2.txt it outputs the correct lines, but also outputs the file name (which it shouldn't).
Last edited on
The logic, the logic. (Well, there are many ways to skin this cat, so this is only one.)

Q: How many command line parameters (words) did the user supply?

IF 0, usage

IF 1, it is a filename OR usage

IF 2, they are two filenames OR usage

IF >2, the two first could be "-n" and a number and the rest should be filenames


Four main cases to handle. Most of them require additional tests.


Trivia: What does the line 6 of your code do?
line 6 was to print the file name to the screen as well, i deleted this since
So is this all I need for the usage statement to print?
Last edited on
Pages: 12