Reading a file given as a command line argument via argv - how?

ceeadf used Ask the Experts™
I am undertaking this excercise where you have to read in 2 files given as command line arguments via argV. This is what I am stuck at - how do I read in a file using argv?
argv[0] = the program
argv[1] = file 1
argv[2] = file 2

How do I open the files and read the contents?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

You do use the argv stuff as file names, and you better make a copy of the argv stuff and do not modify that array.

So you check if the right number of parameters is there.

char file_name1[SOME_LENGTH];
char file_name2[SOME_LENGTH];

strnpy(file_name1, argv[1];
strncpy(file_name2, argv[2];

now you try to open the files with fopen
FILE *fd1 = fopen(file_name1, "r");
check if that succeeded open and fd2 in a simular way and than you can use
fgets to read in line by line
or fscanf for formatted input from a file or



Sorry I am not really understanding. I have a file called "nums". This file has to be read in as a command line argument via argv. How do I do this and read the first number in the file?


Also how does the compiler know where to find the file - where do I indicate the path of the file?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


Question one how to read in arguments from the command line
the prototy for main is
int main(int argc, char* argv[]}

argv[0] is usually the program name
argv[1] is the first parmater

you can use is that way:
int main (int argc, char* argv[]){
  int i = 0;
  for (i = 1; i < argc; ++i){
    printf ("%s ", argv[i]);
  fputc('\n', stdout);
  return 0;

second question how does C find the file?
C doesn't that's up to you. you've to provide it on the command line

./my_program path/to/my/file could be a valid path to your file

If you try to open if with fopen and it can not be found you will get a return value NULL which indicates that the file can not be found or that you are not allowd to access it.


Dear friend use

main(int argc,char ** argv) {
FILE *prog,*fp1,*fp2;
int i;

if(argc<2) {
   printf("\n pl give two file names \n");

if((prog=(FILE *) fopen(argv[0],"r"))==NULL)
  printf("\nUnable to open : %s ",argv[0]);
if((fp1=(FILE *) fopen(argv[1],"r"))==NULL)
  printf("\nUnable to open file %s ",argv[1]);
if((fp2=(FILE *) fopen(argv[2],"w"))==NULL)
  printf("\nunable to open file %s ",argv[2]);

/* rest of code goes here
U can change the mode option given to fopen function depending upon ur requirements */

another generic way .. when u dont know how many file names u gonna get ..

#include <stdio.h>

void readFile(char *path){
FILE *fp;
process the contents of the file
perror("file couldnt be opened");

main(int argc,char** argv){
int i;
if(argc>1){// got some files to read
I'm having the same problems as you I think

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial