C ++ How to pass command line argument to read txt file

What I am trying to do is ...

1) to read txt files by command line argument,

2) use strings in txt files as arguments to main method (or any other method that needs to be called).

For example, there are two txt files, one of which is named character.txt and the other match.txt.

The content of the files will be like this.

character.txt

//This comprises of six rows. Each of the rows has two string values
Goku Saiyan
Gohan Half_Saiyan
Kuririn Human
Piccolo Namekian
Frieza villain
Cell villain

      

match.txt

//This comprises of three rows, each of them is one string value
Goku Piccolo
Gohan Cell
Kuririn Frieza

      

If I use these lines without using the command line, I would declare the lines in the character.txt file as follows.

typedef string name; //e.g. Goku
typedef string type; //e.g. Saiyan, Human, etc

      

Now I'm looking for how to read and send string values ​​from txt files like the ones above and use them for functions inside the main method, ideally in this way.

int main(int argc,  char *argv)
{
    for (int i = 1; i < argc; i++) {

        String name = *argv[i]; //e.g. Goku
        String type = *argv[i]; //e.g. Saiyan, Human, etc
        String match = * argv[i]; //Goku Piccolo
        //I don't think any of the statements above would be correct.
        //I'm just searching for how to use string values of txt files in such a way

        cout << i << " " << endl; //I'd like to show names, types or matchs inside the double quotation mark. 
    }
}

      

Ideally, I would like to call this method this way. enter image description here

According to this website. at least I understand that command line arguments can be used with C ++, but I cannot find more information. I would appreciate it if you could advise him.

PS. I am using windows and code blocks.

+3


source to share


4 answers


Assuming you just want to read the contents of the files and process it, you can start with this code (without any tho check errors). It just gets the filenames from the command line and reads the contents of the file by 2 vectors. Then you can simply process those vectors as needed.



#include <string>
#include <fstream>
#include <iostream>
#include <vector>

std::vector<std::string> readFileToVector(const std::string& filename)
{
    std::ifstream source;
    source.open(filename);
    std::vector<std::string> lines;
    std::string line;
    while (std::getline(source, line))
    {
        lines.push_back(line);
    }
    return lines;
}

void displayVector(const std::vector<std::string&> v)
{
    for (int i(0); i != v.size(); ++i)
        std::cout << "\n" << v[i];
}

int main(int argc,  char **argv)
{
    std::string charactersFilename(argv[1]);
    std::string matchesFilename(argv[2]);
    std::vector<std::string> characters = readFileToVector(charactersFilename);
    std::vector<std::string> matches = readFileToVector(matchesFilename);

    displayVector(characters);
    displayVector(matches);
}

      

+2


source


to see how to use command line arguments.

http://www.cplusplus.com/articles/DEN36Up4/

you cannot use the contents of the file that you passed to the application using command line arguments. only the filename is passed to the application.



you should open the file using this name and read its contents. take a look at this:

http://www.cplusplus.com/doc/tutorial/files/

+2


source


You are defining the prototype incorrectly main

. You also need std::ifstream

to read the files.

If you are expecting exactly two arguments, you can check argc

and extract the arguments directly:

int main(int argc, char* argv[]) {
    if(argc != 3) {
        std::cerr << "Usage: " << argv[0] 
                << " name.txt match.txt" << std::endl;
        return 1;
    }

    std::ifstream name_file(argv[1]);
    std::ifstream match_file(argv[2]);

    // ...

    return 0;
}

      

If you are expecting an indefinite number of files, you need a loop and an array to store them, i.e. vector

:

int main(int argc, char* argv[]) {
    std::vector<std::ifstream> files;

    for(int i = 1; i < argc; ++i) 
        files.emplace_back(argv[i]);

    // ...

    return 0;
}

      

And don't forget to check if the files are open.

+1


source


First, the main prototype of the function should be

    int main(int argc, char **argv)

      

OR

    int main(int argc, char *argv[])

      

After getting the filenames in the main function, you must open each file and get its contents

Third example code

    int main(int argc, char* argv[])
    {
        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);
               //your logic here
               f.close();
           }
     return 0;
     }

      

+1


source







All Articles