简体   繁体   中英

C++ Convert filename input redirection to new output text file with .txt appended to name of new output file

I am trying to use input redirection to scan a file for regular expressions and output those regular expressions with the line number of the file to a new output text file. The output text file is to be the name of the file that was used for the input redirection with ".txt" appended to it. For instance if the program was run as follows:

./scanRegex < scanThisFile.log

Then the output file should be called

scanThisFile.log.txt

I created a simple program as follows ( minus the regex scanning to isolate the issue ).

main.cpp

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

    int main( int argc, char* argv[] )
    {
       std::string fileName = argv[1]; //<---===== ??????
       std::string txt = ".txt\n";
       char outputFile[100];

       for( int i = 0; i < fileName.length(); ++i ){
         outputFile[i] = fileName[i];
       }
       for( int i = fileName.length(); i < fileName.length() + 4; ++i ){
         outputFile[i] = txt[i - fileName.length()];
       }

       std::ofstream outfile;
       outfile.open(outputFile);

       outfile << "It works!!";

       outfile.close();
    }

When I use

argv[ 0 ]

the program runs but the filename is wrong for what my intention is but is understandable because the program name is the first argument for argv: a.txt

When I use

argv[ 1 ]

I get the following runtime error:

osboxes@osboxes:~/Desktop/ps7$ ./a < device1_intouch.log terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped)

When I use

argv[2]

the program runs but the filename is wrong and full of gibberish (overflow?):

Maybe this is only part of where my problem is. Any help would be greatly appreciated. Thank you.

You're confusing the standard input with your program's command line arguments . Command line arguments are the list of strings that you include on the command line when you invoke your program. For example:

$ ./myProgram arg1 arg2 ... argn

These are read via argv and argc , which respectively represent the "argument vector" and "argument count." By convention, the first argument is the working directory of your program. In this example, you would have:

argv == {"/path/to/myProgram", "arg1", "arg2", ..., "argn"}
argc == n

at the start of main . You must be careful not to read from argv out of bounds by checking argc , as with any raw array.

The standard input , on the other hand, is a stream of data that is supplied to your program throughout the call to main . This is read using std::cin .

int main(int argc, char** argv){
    std::string s;
    std::cin >> s; // read from standard input
}

When you run this program, it will block at the indicated line until it recieves data from the standard input. This data can be supplied by typing manually while the program is running from the command line:

$ ./myProgram

hello

or by input redirection:

$ echo "hello" | ./myProgram

$ ./myProgram < hello.txt

In the three examples above, s will contain the first word of text from the input, and you can use it for whatever you want on the following line.

Note that std::cin >> s will read text until it reaches the first white-space character. Fortunately, there are easy ways to read an entire line from stdin and to read everything from stdin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM