Thread: NewB Problem

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    NewB Problem

    Ok all please help an OLD VAX Fortran programmer understand why this does not work.
    The is idea is to read the command line and parse it so that i can extract a file name to record the data to. Also, i need to be able to get the other arguments to work. This segment of code was copied from a similiar vax fortran program.

    input to command line >>>> dr /r grr1.dat /? /p 1 /s 9600

    output==>>
    recording data to: grr1.dat
    port 1
    baud rate 9600
    at 12:43pm


    int main(int argc, char* argv[])
    { int count;
    printf("argument count %d \n",argc);
    for( count = 0; count < argc; count++ )
    {
    printf( " argv[%d] %s\n", count, argv[count] );
    if ( argv[count] == "/r" )
    {
    printf("call recording\n");
    printf("file %s \n",argv[count+1]);
    }
    if ( argv[count] == "/?" ) printf("call usage\n");
    }

    return 0;

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Quickly read thru your code

    spotted this ...
    Code:
    printf( " argv[%d] %s\n", count, argv[count] ); 
    /*should print charcter  %c not string */
    printf( " argv[%d] %c\n", count, argv[count] ); 
    /*same here */
    printf("file %s \n",argv[count+1]); 
    
    printf("file %c \n",argv[count+1]);
    Last edited by bigtamscot; 01-27-2002 at 04:09 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    int main(int argc, char* argv[]) 
    {  int count; 
       printf("argument count %d \n",argc); 
       for( count = 0; count < argc; count++ ) 
       { 
          printf( " argv[%d] %s\n", count, argv[count] );
          /*Won't work, comparing strings with ==*/ 
          if ( argv[count] == "/r" ) 
          { 
             printf("call recording\n");
             /*I'm assuming the command line arguments 
             **are not interchangeable. If they are this could 
             **choke.
             */ 
             printf("file %s \n",argv[count+1]); 
          }
          /*Comparing strings with == won't work*/ 
          if ( argv[count] == "/?" ) printf("call usage\n"); 
       } 
    
       return 0;
    }
    To compare strings you want to use either strcmp or strncmp, == won't work. And make sure that if the command line arguments are interchangeable you test that the next string in argv isn't nothing or calling argv[count + 1] will cause a nasty bug.

    bigtamscot:
    argv is defined as char *argv[] which means it's an array of pointers to char, aka an array of strings. Using the format key %c for a string constant won't work as expected.

    -Prelude
    Last edited by Prelude; 01-27-2002 at 05:33 PM.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(int argc, char* argv[])
    {
       while (argc--)
       {
          if (!strncmp(argv[argc], "/r", 2))
          {
             printf("data file: %s\n", argv[argc+1]);
          }
          else if (!strncmp(argv[argc], "/p", 2))
          {
             printf("port: %s\n", argv[argc+1]);
          }
          else if (!strncmp(argv[argc], "/s", 2))
          {
             printf("speed: %s\n", argv[argc+1]);
          }
       }		
    
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    2
    Thank you for help ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM