mercredi 27 juillet 2016

Using argv to read a text file

I am trying to read a text file using argv[1]. In my code after building argc remains 1 only that means the file is not detected. Please see my code below:

#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
 int main(int argc, char **argv) {
    char msg[256]={0};
    if (argc < 2){
        cout<<"file not found";
    }
    if(argc > 1){
        strcpy(msg,argv[1]);
    }
     FILE *file;
     if ((file = fopen(argv[1], "r")) == NULL)
         {
             printf("file doesnt exist\n");
         }
     fclose(file);

    cout<<msg;

}

I am not able to figure out how do I get the value of argc greater than 1 and then read the content of the file? Please help me out here.

Aucun commentaire:

Enregistrer un commentaire