user5999727
user5999727

Reputation: 21

Command line argument to read text file

I want to be able to read text files from the command line but my program is not compiling. Does anyone know what I'm doing wrong?

I'm trying to have the command like accept the menu.txt files and read them and put them into an array but I don't know how to have it read them from the command line

So what I'm trying to do is

1) ./a.out menu1.txt menu2.txt

And let the user choose how many files they want to read from so it could also be

2) ./a.out menu1.txt menu2.txt menu3.txt how do I do that?

menu1.txt 
hamburger 5.00
pizza 3.25
chips 2.50

menu2.txt
hamburger 2.00
pizza 2.35
chips 1.50

menu3.txt
hamburger 4.00
pizza 5.35
chips 0.50

This is what i have so far:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main( int argc, char* argv[] ){
    ifstream inStream;
    ofstream outStream;
    for(int i=1; i <= argc; i++) 
    // i=1, assuming files arguments are right after the executable
    {
        string fn = argv[i]; //filename
        cout << fn;
        fstream f;
        f.open(fn);


        //inStream.open("menu1.txt");
        // inStream.open("menu2.txt");
        string item[10];
        double cost[10];


        // ifstream m1("menu1.txt");
        //ifstream m2("menu2.txt");


        string name;
        double var;

        string line;
        istringstream iss(line);

        short loop=0;
        if(f.is_open()){

            while(!f.eof()){
                iss >> name >> var;
                getline(f, line);
                while(f>>name>>var){
                    item[loop]=name;
                    cost[loop]=var;
                    cout << "Item at index " << loop << " " << item[loop]<<endl;
                    cout << "Cost at index " << loop << " " << cost[loop]<<endl;
                    loop++;
                }

            }

        f.close();
        }


    }


    return 0;

}

These are the error messages when i try to compile:

     p3.cpp: In function ‘int main(int, char**)’:
     p3.cpp:17:25: error: no matching function for call to      ‘std::basic_ifstream<char>::open(std::string&)’
         inStream.open(fn);
                         ^
p3.cpp:17:25: note: candidate is:
In file included from p3.cpp:1:0:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/fstream:541:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       open(const char* __s, ios_base::openmode __mode = ios_base::in)
       ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/fstream:541:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’

Upvotes: 1

Views: 6429

Answers (3)

Sagar Bahadur Tamang
Sagar Bahadur Tamang

Reputation: 2709

Here is the solution.

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main( int argc, char* argv[] ){
ifstream inStream;
ofstream outStream;
for(int i=1; i < argc; i++){
    cout  << endl << argv[i];
    fstream f;
    f.open(argv[i]);


    //inStream.open("menu1.txt");
   // inStream.open("menu2.txt");
    string item[10];
    double cost[10];


    // ifstream m1("menu1.txt");
   //ifstream m2("menu2.txt");


    string name;
    double var;

    string line;
    istringstream iss(line);

    short loop=0;
    if(f.is_open()){

        while(!f.eof()){
            iss >> name >> var;
            getline(f, line);
            while(f>>name>>var){
                item[loop]=name;
                cost[loop]=var;
                cout << endl << "Item at index " << loop << " " << item[loop];
                cout << endl << "Cost at index " << loop << " " << cost[loop];
                loop++;
            }

        }
        cout << endl;

    f.close();
    }


}


return 0;
}

Upvotes: 0

Aeonos
Aeonos

Reputation: 387

You are trying to pass the filename as a string to fstream::open which only takes

open(const char* __s, ios_base::openmode __mode = ios_base::in)

as argument, what your compiler is trying to tell you. Which means a pointer to a char array. So it would be better to parse the arguemtns as such:

const char * fn = argv[i]

Another way would be to enable c++11 support, because since than the open method also accepts a std::string as argument: http://www.cplusplus.com/reference/fstream/fstream/open/

One could also convert the string to a c-style string and call the function:

f.open(fn.c_str());

And as another advice, please dont use:

using namespace std;

as it can lead to horrible problems.

Upvotes: 2

Rishi
Rishi

Reputation: 1395

You are converting argv[i] to a string which is not required. You can do this instead.

const char *fn = argv[i]; 
// other code    
f.open(fn);

fstream::open accepts a const char* filename argument.

Upvotes: 1

Related Questions