Link to home
Start Free TrialLog in
Avatar of ceeadf
ceeadf

asked on

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

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?



Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

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

Regards
Friedrich






Avatar of ceeadf
ceeadf

ASKER

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?
Avatar of ceeadf

ASKER

Also how does the compiler know where to find the file - where do I indicate the path of the file?
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);
  fflush(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.

Regards
Friedrich
Dear friend use
#include<stdio.h>

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

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

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;
fp=fopen(path,"r");
if(fp!=NULL){
/*
process the contents of the file
*/
fclose(fp);
}else{
perror("file couldnt be opened");
}
}

main(int argc,char** argv){
int i;
if(argc>1){// got some files to read
for(i=1;i<argc;i++)
readFile(argv[i]);
}
return;
}
ASKER CERTIFIED SOLUTION
Avatar of AEaston
AEaston

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial