LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to pass a file name into the main Method (C/C++) (https://www.linuxquestions.org/questions/programming-9/how-to-pass-a-file-name-into-the-main-method-c-c-23927/)

shassouneh 06-20-2002 05:14 PM

How to pass a file name into the main Method (C/C++)
 
Dear All,
I wish to pass the name of a file into the main function such that the program can be called like this:

programname filename.

I want to write a simple program to count the number of lines of text in a text file. So the syntax should be something like:

linecounter filename

Any ideas please?

acid_kewpie 06-20-2002 07:01 PM

how about you look at a C manual for a start? ideas such as this a pretty fundamental to C, getting to grips with them is a must.

anyway... you get the main(int argc, char **argv) line, so argc is teh number of parameters or whatever, and the argv is a pointer to the array of parameters. just access those two vairables and pull out the data you need and go willd with fopen() and such like

shassouneh 06-20-2002 08:07 PM

cool. thanx :)


All times are GMT -5. The time now is 02:21 PM.