write a loop to a file, getopt()

This is a discussion on write a loop to a file, getopt() within the C Programming forums, part of the General Programming Boards category; I decided today to re-write a simple calculator I wrote a while back in Java, to C. This time I'm ...

  1. #1
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    write a loop to a file, getopt()

    I decided today to re-write a simple calculator I wrote a while back in Java, to C. This time I'm trying to make the calculator a bit more advanced with a few command line options. Among many a -v option for verbose. When the -v option is set I want the output from the calculations (the -c option start the loop) to be written to a file. But there's a few problems. I'm using the getopt() function to retrieve the options set. If I use ./calc -vc (or -v -c) the verbose mode is on, and the calculation loop start. But if I do ./calc -cv (or -c -v), only the calculation loop starts. Why? The real problem here is I don't know how to write the input/output from the loop to a file. Should I create a buffer? How do I tell (check) the loop that the -v option is set?
    Here's some of the code (I have cut most of it off, if more is needed let me know):
    Code:
        while((argument = getopt(argc, argv, "hvcV")) != -1) {
    
            switch(argument) {
    
                case 'h':
                    help();
                    break;
    
                case 'v':
                    printf("verbose mode on\n");
                    verbose();
                    break;
    
                case 'c':
                    printf("options:\t+ - / *\n");
                    calculate();
                    break;
    A part of the calculate(), which loops until either Q or q is pressed:
    Code:
            switch(option) {
    
                case '+':
                case '1':
                    printf("first value:\t");
                    first = userInput();
                    printf("second value:\t");
                    second = userInput();
                    total = first + second;
                    printf("total:\t\t%.3f\n", total);
                    break;
    Any suggestions? Thanks in advance.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> But if I do ./calc -cv (or -c -v), only the calculation loop starts. Why?
    Because getopt() hits the c option first which calls calculate() directly. getopt() is then called again and hits the v option, but by then it's too late.

    Do something like:

    Code:
    int optCalculate = 0;
    int optVerbose = 0;
    
    while((argument = getopt(argc, argv, "hvcV")) != -1) {
    
            switch(argument) {
    
                case 'h':
                    help();
                    break;
    
                case 'v':
                    printf("verbose mode on\n");
                    optVerbose = 1;
                    break;
    
                case 'c':
                    optCalculate = 1;
                    break;
    }
    
    if (optVerbose) printf ("I'll tell you what I'm doing...\n");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Sorry for the delay in replying.

    I don't think I completely understood what you ment. I tried the following (note that the verbose() calls the calculate() again):
    Code:
    int setVerbose = 0;
    [...]
    
    while((argument = getopt(argc, argv, "hvcdV")) != -1) {
    
     switch(argument) {
    
       case 'h':
         help();
         break;
    
       case 'v':
         setVerbose = 1;
         break;
    
       case 'c':
         printf("options:\t+ - / *\n");
    
         if(setVerbose == 1) {
           printf("verbose mode:\ton\n");
           verbose();                    
         } else {
           printf("verbose mode:\toff\n");
           calculate();
         }
         break;
    The problem is I can see this isn't a good sollutuion. It doesn't work if you use -c -v, only with -v -c. In theory the ideal thing would be to place the if sentence after all the options, but is that a good idea?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>place the if sentence after all the options, but is that a good idea?<<
    In essence, yes.

    Normally, I'd finish parsing the args before invoking any functions that do any real work. It would be acceptable to put code like puts("verbose mode is now on"); in the loop you've shown though.

    So, maybe it'd look like this:
    Code:
    while((argument = getopt(argc, argv, "hvcdV")) != -1) {
    
     switch(argument) {
    
       case 'h':
         help();
         return 0;  /* End of program */
         break;
    
       case 'v':
         setVerbose = 1;
         puts ("Verbose mode is on");
         break;
    
       case 'c':
         DoCalculate = 1;
         break;
    }
    
    if (DoCalculate)
    {
         if (setVerbose) puts ("Calling Calculate()");
         Calculate();
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doesn't write in file when call GetSaveFileName()
    By randall81 in forum Windows Programming
    Replies: 1
    Last Post: 03-28-2009, 02:34 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21