Thread: Command line

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    Command line

    This is a read and writing the binary data to and from a file program. The program has to take command line arguments to specify the input and output files to use.

    This means that I have several valid ways to invoke the program. Argument order should not matter.
    ./binaryio -i input.dat -o output.dat
    ./binaryio -o output.dat -i input.dat
    ./binaryio -i foo.dat -o foo.dat

    my binary file im reading from is "pewp.dat" (sorry about the immature file name, i am a little immature at times)

    When i run my program this way "./binaryio -i pewp.dat -o output.dat" it runs without any errors and creates my output.dat file and writes what was in my pewp.dat file.

    but when i "./binaryio -o output.dat -i input.dat" i get a seg fault, not really sure why its giving me that maybe someone can help me.

    here is the codez
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void sort(int arr[], int *num_items);
    
    int main(int argc, char * argv[])
    {
            int cap = 10;
            int *arr = malloc( cap * sizeof(int));
            int i, num_items;
            FILE * fptr;
    
            for(i=1; i < argc; ++i)
            {
                    if(strcmp(argv[i], "-i"))
                    {
                            fptr = fopen(argv[i], "r");
                            num_items = fread ( arr, sizeof( int ), cap, fptr );
    
                            printf( "number of items read = %d\n", num_items );
    
                            sort( arr, &num_items );
    
                            for( i=0; i<num_items; ++i)
                            {
                                    printf("%d ", arr[i]);
                            }
                            printf("\n");
    
                            fclose( fptr );
                    }
    
                      if(strcmp(argv[i], "-o"))
                    {
                            fptr = fopen(argv[i], "w");
                            fwrite ( arr, sizeof( int ), cap , fptr );
    
                            fclose( fptr );
                    }
    
            }
    
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    if(strcmp(argv[i], "-i"))
                    {
                            fptr = fopen(argv[i], "r");
    You're trying to open "-i" here.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    what do you mean im tying to open "-i"

    can you explain a little more please?
    Last edited by Dr Saucie; 02-23-2010 at 12:31 PM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    EDIT: Wrong.
    Last edited by Memloop; 02-23-2010 at 12:46 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1 The above problem mentioned by Memloop also exists for the output file as well.

    #2
    Code:
    if(strcmp(argv[i], "-i"))
    
    ...
    
    if(strcmp(argv[i], "-o"))
    strcmp returns 0 if the strings match and a nonzero value if they don't match. This means the above "if" tests are wrong as strings matching "-i" or "-o" will not cause the related if blocks to be entered.

    #3 Assuming you encounter "-o" first as an argument (and you correct the strcmp and fopen issues), you'll write the random contents of your dynamically created array. Is this intended behavior or should you first read the contents of the input file first before outputting?

    #4 You never free your dynamically allocated array.

    #5 There is no error checking for the malloc and fopen calls. You assume that they succeed.

    [edit]
    Quote Originally Posted by Dr Saucie
    what do you mean im tying to open "-i"

    can you explain a little more please?
    He means that i is not incremented in between when you check it to be "-i" or "-o" and therefore remains the same when you get to the fopen calls. You are therefore attempting to open files named "-i" and "-o". You need to increment i after the strcmp checks (prior to calling fopen).[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    thanks guys ill read up a bit more on strcmp, but im pretty sure i know what you mean.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Also you can save your sanity on the command line by using something called Example of Getopt - The GNU C Library. It is common/free on Unix-like systems but I have seen implementations for Windows as well and can make the difference when parsing non-trivial amounts of program arguments..here is one for Windows:
    C Consultant : Contract Software Engineering : Free Sample Code : http://www.pwilson.net/
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed