c read file from command line

#include <stdio.h>

int main(int argc, char **argv) {
    for (int i = 0; i < argc; ++i) {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
}

/*
	[birryree@lilun c_code]$ ./a.out hello there
	argv[0]: ./a.out
	argv[1]: hello
	argv[2]: there
*/

3
2

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

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

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

To read the file:
FILE *f = fopen(argv[1], &quot;r&quot;);

3 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
read file c from commandline c read file from command line reading from command line in c reading from command line c how to open a text file in C reading files in c using command line read file from command line c how to open and read into command line from a C program how to read argv in c run code with main arguments argv example c reading files in c c read the file comand line to open file in c language take input from file in c in command line opening a textfile with command line arguments in c how to read cmdline file in c how to read form cmd line in c inputting file from argv1 c use argv passing command line arguments while compiling c code argc example read in a file from the command c c read .vert file c language argument file windows library c read file read from a file line by line in c c read file from second line reading file in c from line 2 read file in c line by line c file read and write how to reead from a file inputting in the cmd c read integer from command line c a C file that uses fopen for reading the file. reading a filename from command line argument in c c program to read a file readfile c read and orint a line from file C read from command line c how to insert an arguement inside argc in codeblock input file from command line c how to read lines of a file in c read lines from file c read line from file c Pass arguments into C program from linux pass arguments to c program linux linux command line arguments into c program implement command line operations in c main(int argc char**argv) in c language read from file in c c read from argv c read file c get parameter of ... read command in c how to read a file from command line in c read file line by line c get arguments from command line c how to read a file line by line in c c get console args how to read command line arguments in file argv doesn't recognize input from file c optional command line arguments in c
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source