Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C File Processing

    Hi,

    I'm making a program that will read information from a file, and when exited, will write to a new file. These files are to be inputted at the time of typing the executable file.
    eg <executable file> <file.inp> <file.out>

    Now, i know how to read from/print to the files using fopen/close, fgets/puts, sscanf/sprintf, but what i dont know is how to actually let the program know that the words typed after the executable are the names of the 2 files.

    Does it have to do with putting declarations inside the main brackets?
    eg
    int main( xxx, yyy)


    I tried this but kept getting errors as it says the first and second declarations need to be an int and **.
    Any help would be very much appreciated.

    Thanks.

  • #2
    New Coder
    Join Date
    Sep 2002
    Location
    Up North (UK)
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dole_4,

    Yes the parameters in the main function have the parameters supplied after the command.
    Code:
    int main( int argc, char* argv[])
    The argc parameter details the number of parameters given on the command line. This is always at least 1 as the name of the program, as typed by the user, is the first parameter. The argv parameter is an array of character strings on the command line. The following program demonstrates how to access the parameters.
    Code:
    int main(int argc, char* argv[])
    {
    	int i = 0;
    	printf("Numb of parameters: %i\n", argc );
    	for( i = 0; i < argc; i++ ) printf( "Par %i: %s\n", i, argv[i]);
    	return 0;
    }
    Hope this helps.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it is done with argument count (int argc) and argument vamue (char *argv[])
    argv[0] contains the name of your exe. this is argumant 0

    here is an example program:
    Code:
    #include <stdio.h> 
    
    int main(int argc, char *argv[])
    {
    printf(" the arguments are: ");
    for(int i=0;i<argc;i++)
    {
    	printf("\n%s\n",argv[i]);
    }
    return 0; 
    
    }

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    damn too late

    fivesidecube, next time, type slower.

  • #5
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks

    yep that helps ...

    thanks alot


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •