C 'Main' args and types

This is a discussion on C 'Main' args and types within the C Programming forums, part of the General Programming Boards category; i'm using the method void main(int NumberOfArguments, char* Arguments[]) { } to accept a list of input arguments which contain ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    C 'Main' args and types

    i'm using the method

    void main(int NumberOfArguments, char* Arguments[])
    {
    }

    to accept a list of input arguments which contain filenames.

    1. How do i address these variables with fopen('filename', 'accesstype') such that i am sending the actual array of char to the 'filename' variable instead of it's memory address?
    The filenames are using the drive:/path/*/filename method to avoid double backslashes.

    2. Is it possible for the filename to contain spaces without them being assigned as a new argument?

    3. Is the variable in Arguments[0] always the name of the program and Arguments[1] the first argument?

    Thanks in advance,
    Shaun

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    int main(int argc, char *argv[])
    {
        int i;
        for (i = 1; i < argc; i++)
            puts(argv[i]);
        return 0;
    }
    >void main
    Is a no no. Use int main and return something at the end.

    >2. Is it possible for the filename to contain spaces without them being assigned as a new argument?
    Yes, start your program like this:
    >myprog "my file name with spaces.txt"
    ... basically, use quotes to surround text to make it into a single arg.

    >3. Is the variable in Arguments[0] always the name of the program and Arguments[1] the first argument?
    Yes. Sometimes argv[0] contains a path name as well as the prog filename.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    Thanks for the info there, any idea about the first one? how to pass the actual array of char as opposed to the pointer to the memory location?

    example, i'm passing the filenames to a new section before opening them. assuming i kept the name Arguments and passed Arguments[1] eg.


    in the main method call the following method with

    [start of code]

    OpenThisFile(Arguments[1]);


    void OpenThisFile(char* FileName) {

    FILE *inputfile = fopen(FileName, "r");

    }

    [end of code]

    also, how do i convert char* to the required type in the fopen() section. i think it's meant to be something other than char*

    Have a good one,
    Shaun

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    FILE *fopen( const char *filename,
                 const char *mode );
    See? The first arg of fopen() is a char* (with a const option). Why don't you compile your code and see what it does.

    Have a read of this too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by KawaiiCB
    Thanks for the info there, any idea about the first one? how to pass the actual array of char as opposed to the pointer to the memory location?

    example, i'm passing the filenames to a new section before opening them. assuming i kept the name Arguments and passed Arguments[1] eg.


    in the main method call the following method with

    [start of code]

    OpenThisFile(Arguments[1]);


    void OpenThisFile(char* FileName) {

    FILE *inputfile = fopen(FileName, "r");

    }

    [end of code]

    also, how do i convert char* to the required type in the fopen() section. i think it's meant to be something other than char*

    Have a good one,
    Shaun
    FILE *foobar = fopen (argv[1], "r");

    i'll forego the whole array vs pointer thing because it's really too complicated and not that useful, just know that the above construct works.
    hello, internet!

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    Thanks again for all the help, the code you gave me does work, only i now have to get it to work this way.

    Code:
    void CheckArgs(int iNumberOfArgs, char* iArguments[])
    {	
    		printf(&iArguments[0]);
    		printf(&iArguments[1]);
    		printf(&iArguments[2]);
    		MyCopyFile(iArguments[1], iArguments[2]);
    }
    
    
    
    int main(int iNumberOfArgs, char* iArguments[])
    {
    	srand(time(NULL));
    	rand();
    	if (iNumberOfArgs > 4 || iNumberOfArgs < 2)
    	{
    		printf("USAGE: [[/C] | [/D]] INPUTFILE [OUTPUTFILE]\n");
    	}
    	else
    	{
    		if (iNumberOfArgs == 2) CheckArgs(1,iArguments[1]);
    		if (iNumberOfArgs == 3) CheckArgs(2, (iArguments[1], " ", iArguments[2]));
    		if (iNumberOfArgs == 4) 
    		{
    			CheckArgs(3, iArguments[1], " ", iArguments[2], " ", iArguments[3]);
    		}
    
    	}
    	return 0;
    }
    The other problem is that the printf() returns the right hand sides of the arguments. the first argument it in full, the second is missing a letter on the left and the third is missing two letters on left.

    any ideas??

    thanks,
    Shaun

    The problem i'm having is that by that point, it is pointing to a memory locaiton with a reference to a memory location in it.

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by KawaiiCB
    Thanks again for all the help, the code you gave me does work, only i now have to get it to work this way.

    Code:
    void CheckArgs(int iNumberOfArgs, char* iArguments[])
    {	
    		printf(&iArguments[0]);
    		printf(&iArguments[1]);
    		printf(&iArguments[2]);
    		MyCopyFile(iArguments[1], iArguments[2]);
    }
    
    
    
    int main(int iNumberOfArgs, char* iArguments[])
    {
    	srand(time(NULL));
    	rand();
    	if (iNumberOfArgs > 4 || iNumberOfArgs < 2)
    	{
    		printf("USAGE: [[/C] | [/D]] INPUTFILE [OUTPUTFILE]\n");
    	}
    	else
    	{
    		if (iNumberOfArgs == 2) CheckArgs(1,iArguments[1]);
    		if (iNumberOfArgs == 3) CheckArgs(2, (iArguments[1], " ", iArguments[2]));
    		if (iNumberOfArgs == 4) 
    		{
    			CheckArgs(3, iArguments[1], " ", iArguments[2], " ", iArguments[3]);
    		}
    
    	}
    	return 0;
    }
    The other problem is that the printf() returns the right hand sides of the arguments. the first argument it in full, the second is missing a letter on the left and the third is missing two letters on left.

    any ideas??

    thanks,
    Shaun

    The problem i'm having is that by that point, it is pointing to a memory locaiton with a reference to a memory location in it.
    what exactly are you trying to do here? this code has many problems in it.
    hello, internet!

Popular pages Recent additions subscribe to a feed

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