Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line arguments, reading a file

If i entered into the command line C: myprogram myfile.txt

How can I use myfile in my program. Do I have to scanf it in or is there an arbitrary way of accessing it.

My question is how can I use the myfile.txt in my program.

int
main(){
    /* So in this area how do I access the myfile.txt 
    to then be able to read from it./*
like image 243
user2440329 Avatar asked Jun 01 '13 05:06

user2440329


People also ask

How do you read command line arguments?

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.

How do you read a command line argument in Java?

Core Java bootcamp program with Hands on practice A command-line argument is an information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy. They are stored as strings in the String array passed to main( ).

What is command line arguments with example?

A command-line argument is nothing but the information that we pass after typing the name of the Java program during the program execution. These arguments get stored as Strings in a String array that is passed to the main() function. We can use these command-line arguments as input in our Java program.


2 Answers

You can use int main(int argc, char **argv) as your main function.

argc - will be the count of input arguments to your program.
argv - will be a pointer to all the input arguments.

So, if you entered C:\myprogram myfile.txt to run your program:

  • argc will be 2
  • argv[0] will be myprogram.
  • argv[1] will be myfile.txt.

More details can be found here

To read the file:
FILE *f = fopen(argv[1], "r"); // "r" for read

For opening the file in other modes, read this.

like image 99
srikanta Avatar answered Sep 22 '22 19:09

srikanta


  1. Declare your main like this

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

    • argc specified the number of arguments (if no arguments are passed it's equal to 1 for the name of the program)

    • argv is a pointer to an array of strings (containing at least one member - the name of the program)

    • you would read the file from the command line like so: C:\my_program input_file.txt

  2. Set up a file handle:

    File* file_handle;

  3. Open the file_handle for reading:

    file_handle = fopen(argv[1], "r");

    • fopen returns a pointer to a file or NULL if the file doesn't exist. argv1, contains the file you want to read as an argument

    • "r" means that you open the file for reading (more on the other modes here)

  4. Read the contents using for example fgets:

    fgets (buffer_to_store_data_in , 50 , file_handle);

    • you need a char * buffer to store the data in (such as an array of chars), the second argument specifies how much to read and the third is a pointer to a file
  5. Finally close the handle

    fclose(file_handle);

All done :)

like image 32
Nobilis Avatar answered Sep 24 '22 19:09

Nobilis