#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    0
    Rep Power
    0

    Question opening a C file from a specific directory


    a few questions:



    1) i'm writing a program that requires the opening of a file from a specific directory. i know the syntax to create a pointer to a file, however i am unclear as to how i could set the pointer to open a file like this: /home/username/./somedir/filename -- also how do i write the file back to that directory once i've made my editions to it?



    2) how do i call system commands from a C program. for example how would i make the app call xmms for example /usr/bin/xmms ?



    3) what is the syntax for setting up your main() to receive parameters from the command line?



    feedback on any of the above is appreciated. thanks!



    -j
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    1. Specify the complete pathname in the fopen function
    Code:
    #include <stdio.h>
    ....
    ....
    FILE *in, *out;
    /* To open a file in read mode */
    in = fopen("/home/username/./somedir/filename", "r"); 
    
    /* To open a file in write mode */
    out = fopen("/home/username/./somedir/filename2", "w");
    
    /* To open a file in append mode */
    out = fopen("/home/username/./somedir/filename2", "a");
    
    /* To open a file in read/write mode */
    in = fopen("/home/username/./somedir/filename", "r+");
    2. Use the system() call
    Code:
    #include <stdlib.h>
    ....
    ....
    system("/usr/bin/xmms");
    3. Declare the function as:
    int main(int argc, char *argv[])
    or
    int main(int argc, char **argv)

    argc will give you the number of arguments that are passed to the program and the argv array holds the actual arguments. argc will always be >=1 and argv[0] always holds the name of the program itself.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        int x;
    
        for (x = 0; x < argc; x++) 
            printf("Argument #%d is %s\n", x, argv[x]);
    
        return 0;
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    0
    Rep Power
    0
    great, thanks so much ;)

IMN logo majestic logo threadwatch logo seochat tools logo