Thread: argv[], user input

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    Question argv[], user input

    Im stuck with this problem..... (probably an FAQ but couldnt find it there though).
    I know you can pass file names to an argument of main for instance.
    int main( int argc, char argv[]);
    where argv[0] is the filename.

    Now what i want is that the user of my app can shoose wich file he wants to open. That file is needed to let my whole app run.
    So i tried several things which are kinda stupid because i knew they would never work like:
    Code:
    FILE * fl;
    char filename[BUFSIZ];
    fgets(filename,BUFSIZ,stdin);
    fl=fopen("%s","w",filename);
    if(fl==0)
        perror("\n");
    wich obviously wont work since fopen only takes two arguments+ i have the \n character at the end of the string.
    Ive also tried several things with the argv thing. But i cant seem to figure out a way of how to get a string from the user and let the app open that "filename".

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Play with this:
    Code:
    int main(int argc, char *argv[])
    {
        int n;
        
        for (n = 0; n < argc; n++)
            printf("%s\n",argv[n]);
    
        return 0;
    }
    gg

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    if that is anywere near the solution for my prob, then i was almost there myself. But if its done in such a way then ill mess with the argc's and argv's untill it works.
    thx

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Command line args

    >>fgets(filename,BUFSIZ,stdin);
    Just remove the newline with

    char *p;
    if ((p = strchr(filename, '\n')) != NULL) *p = '\0';

    >>fl=fopen("%s","w",filename);
    Make it
    >>fl=fopen(filename,"w");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Thx hammer i figured out that i should use argv[2] (in my case)
    But then i was thinking of a way to get rid of the \n character.
    Thx anyway btw that new faq section is really good.
    Keep it up
    ::edit::
    compile-run-:::speechless--thinking waw was it that simple:::
    thx again Hammer
    ::end_of_edit::
    Last edited by GanglyLamb; 03-24-2003 at 10:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM