passing a file name to main()

This is a discussion on passing a file name to main() within the C Programming forums, part of the General Programming Boards category; i am trying to pass a filename to my program from a promp but i don't know how to pass ...

  1. #1
    Unregistered
    Guest

    passing a file name to main()

    i am trying to pass a filename to my program from a promp but i don't know how to pass a file handling argument to main so that main can open the file and so on. any advices? thanks.

    dj.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Please post your code!

    Show what you are doing...
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    Code:
    int main(int argc,char* argv[])
    {
        char buf[20];
        FILE* fp = fopen(argv[1],"r");
        if(fp == NULL)
            return -1;
        fread(buf,1,20,fp);
        printf("%s\n",buf);
        fclose(fp);
        return 0;
    }
    explaination

    int main(int argc,char* argv)

    this declaration of main tells it to take command line arguments

    where argc is the number of arguements;
    and argv contains the actual arguements

    Note: argv[0] always contains the programs name sothat would be the name of the exe
    Last edited by no-one; 09-05-2001 at 08:33 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    no one's answer contained a typo... main should look like this:

    int main (int argc, char * argv[])

    or

    int main (int argc, char** argv)

    mxr
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    thanks mix0,
    that would have caused some trouble i'll edit and fix it.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    fyi: no-one's code is actually right. in this case char **argv and char *argv[] accomplish the same thing. char* argv[] just means an array of character pointers, each pointer being the base address of a parameter string. either way should work with the compiler.

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    actually i edited it to * argv[] so it would not confuse him. it was when mix0 posted my code was * argv so his correction was quite right.
    Last edited by no-one; 09-06-2001 at 11:28 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    int main(int argc, char *argv[])

    you can use the single star as previously stated all this is doing is point to an unbound array that stores command line inputs, I've always used the single star and not had any problems

    just remember that unlike most arrays argv[] will start at 1 to what ever, this is that the actual program itsself is the argv[0] part.

    so if you entered something like

    compare txt1 txt2 txt3

    argv[0] = compare
    argv[1] = txt1
    argv[2] = txt2
    argv[3] = txt3

    if you are uncertain write a small program that displays the content of the command line input

    Code:
    int main(int argc, char *argv[])
    {
      printf("You enetered the following command prompt %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
    }
    hope this helps

    -ali

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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