Thread: arguments, directories and stuff...

  1. #1

    Question arguments, directories and stuff...

    i have this program... like any program and i would like to make it so that when u type /h after it would go completely silent like that it would print anything on the screen(already covered the code where it doesnt print anything) but i was wondering moe about the commandline arguments and stuff...

    and case two... if my program creates a file... how can i change the directory where it puts them ?

  2. #2
    Registered User
    Join Date
    Jan 2002
    >> i was wondering moe about the commandline arguments and stuff

    int main(int argc, char *argv[])
        if(argc != 0)
            for(int i = argc; i > 0; i--)
                if(strcmp(argv[ i ], "/h")
                    // Put ur dont print anything function here
    i THINK thats how you do it, im not 100% sure, oh and strcmp() needs the string.h file.

    >> how can i change the directory where it puts them ?

    Before you create the file, use this function:

    The SetCurrentDirectory function changes the current directory for the current process. 
    BOOL SetCurrentDirectory(
        LPCTSTR lpPathName 	// address of name of new current directory 


    Points to a null-terminated string that specifies the path to the new current directory. This parameter may be a relative path or a fully qualified path. In either case, the fully qualified path of the specified directory is calculated and stored as the current directory.

    Return Values

    If the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError.


    Each process has a single current directory made up of two parts:

    A disk designator that is either a drive letter followed by a colon, or a server name and share name (\\servername\
    A directory on the disk designator

    See Also

    Or, you could use the system() function with CD, ie:

    system("CD path");

    SetCurrentDirectory() needs windows.h and system() needs stdlib.h
    Last edited by Okiesmokie; 06-26-2002 at 04:01 PM.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    > if(argc != 0)

    Why do you have this check?
    argc will never be zero. It will always be at least 1. The first argument is the name of the executable itself.

    Hope is the first step on the road to disappointment.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002

    Re: arguments, directories and stuff...

    Originally posted by Unregistered
    and case two... if my program creates a file... how can i change the directory where it puts them ?
    you can specifify fully qualified names, like this:
    >fopen("c:\\mydir1\\mydir2\\myfile1", "w");
    >fopen("/home/usr/bob/bobsfile", "w");

    and relative like

    Remember to double up the backslashes if you are using them for DOS files.
    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. Working with directories in windows
    By yajaf125 in forum C Programming
    Replies: 1
    Last Post: 07-28-2008, 03:36 PM
  2. Problem with project directories
    By thetinman in forum Windows Programming
    Replies: 0
    Last Post: 10-27-2005, 01:23 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM