At the start of my program, it should get path to the input file and path to the output file from the console.
But if user give not required number of parameters or wrong parameters (with spaces for example, or without ".txt") it should give the user second chance to enter those parameters without exiting the program. Is it possible?
int main(int argc, char* argv[])
{ //and here should be something to check if the user entered parameters correctly
//(number and if they look like a path) and give a user another try if this is wrong
//(so that user enter them from console again)
string path_open(argv[1]);
strin path_out(argv[2]);
Yes it's possible, but... weird. If you are going to have your program ask for input, why not just put that in a loop until you get proper input? Ultimately, I'd do one or the other:
Get command line input (and, as #IInspectable suggests in the comments, if it's not valid, exit the program); OR
Have program ask for input recursively until the user gives valid input.
Input from command line:
int main(int argc, char* argv[])
{
// sanity check to see if the right amount of arguments were provided:
if (argc < 3)
return 1;
// process arguments:
if (!path_open(argv[1]))
return 1;
if (!path_out(argv[2]))
return 1;
}
bool path_open(const std::string& path)
{
// verify path is correct...
}
Program asks for input:
int main()
{
std::string inputPath, outputPath;
do
{
std::cout << "Insert input path: ";
std::getline(std::cin, inputPath);
std::cout << std::endl;
std::cout << "Insert output path ";
std::getline(std::cin, outputPath);
} while (!(path_open(inputPath) && path_out(outputPath)));
}
Of course you'd validate input separately in case they were entering a valid input path but invalid output path, but you get the gist.
Related
I'm fairly new to C++, I'm trying to figure out how to take in arguments from a text file and use them in my program.
So if I wanted to include a text file whatever.txt in a command, it would use specified lines as arguments.
Like if the text file looked like this:
1 2
and I wanted to use 1 and 2 from it, as arguments in my program.
So far from what I've gathered, I need something akin to this:
int main (int argc, char const *argv[]) {
to start, but not sure where to go from here.
I'm trying to mess around with certain stuff, but I'm still pretty new and outside of loops and things I can't do much with the language yet!
Short of giving you the code that will do this for you (which would ruin the fun of learning how to do these things), the steps I would follow would be:
Take one argument from the command line as the file name you want to read the information from. Think of running your script like this:
$ ./myscript whatever.txt
Then, argv[0]="./myscript" and argv[1]="whatever.txt".
Open the file (perhaps ifstream would be good for this).
Loop through each line of the file (maybe using getline to put each line into a string)
Parse each line of the file (I've seen stringstream used for filling variables from a string).
Hopefully that will help you along your way.
int main(int argc, char *argv[])
after retrieving files names in the main function you should open each file and retrieve its contents
Sample 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;
}
https://stackoverflow.com/a/30141375/3323444
argc is the number of arguments passed in to your program, argv is an array of character strings holding the names of arguments passed in.
at least there's one parameter passed in which is argv[0] which is name of program.
after building this code run it form a command prompt as:
main.exe data.txt
or simply DRAG AND DROP file "data.txt" on your main.exe
main.exe is your program, data.txt is text file containing data you want to use
#include<iostream>
#include<fstream>
int main(int argc, char* argv[])
{
char c;
if(argc < 2)
{
std::cout << "No file passed to program!" << std::endl;
return 1;
}
std::ifstream in (argv[1] , std::ios::in);
if(!in)
std::cout << "Unable to read file: " << argv[1] << std::endl;
while(in >> c)
std::cout << c << std::endl;
in.close();
std::cin.get();
std::cout << std::endl;
return 0;
}
I want to get the input file name in advance, when my program is called, not have my program ask the user for the filename.
I've seen many examples where the program will ask the user for input like:
int main()
{
cout << "Enter file name: ";
cin.get();
//run program
return 0;
}
However, there are not many examples shown where the input is stated in the command line when the program is called:
int main(int argv, char* argc[])
{
ifstream inputfile;
inputfile.open(argc);
//run program
return 0;
}
In other words, the user would type something like this on the terminal:
$./program example.txt
Sorry if my question is like an ELI5 submission.
I think you guys might get the gist of what I am trying to do.
Use:
int main(int argc, char* argv[])
{
ifstream inputfile;
if(argc == 2)
inputfile.open(argv[1]);
else
cout<<"Please nenter filename as command line argument!\n";
//run program
return 0;
}
Here, argc and argv are called command line arguments.
The value of argc is equal to the number of command line arguments, which includes the filename of the executable file itself.
argv is an array of strings passed as command line arguments, where the name of the executable file is at argv[0] and the rest of the arguments follow that.
You need to use:
inputfile.open(argv[1]);
However, I suggest adding checks to the program.
int main(int argc, char* argv[]){
if ( argc < 2 )
{
cerr << "Not enough arguments.\n";
return EXIT_FAILURE;
}
// There is an argument. Use it.
ifstream inputfile;
inputfile.open(argv[1]);
//run program
return 0;
}
I'm writting a c++ program and I am looking to capture the inputs from command line. For each argument that is entered in the command line it is to be captured. The program will capture every command line until the user enters END. The program will then provide an output of each commandline argument that was entered. Example: User enters ./program to run this program. ./program is captured. User then enters test this is then captured user then enters arg then END. The program then outputs
./program
test
arg
This is what I found for a resource I just don't know how to fully implement and loop http://www.cplusplus.com/articles/DEN36Up4/
Below is what I have so far, just not sure of the for loop waiting for the END
thanks!
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
std::cout << "Enter your command line \n and to stop enter END" << std::endl;
for (int i = 0; i < count; ++i)
{
/* code */
}
// Once user types END then arguments entered in will display
std::cout << << std::endl;
return 0;
}
Take a look at cin and getline
Sample code (not tested):
string command_list;
while(true) {
string input;
getline (cin, input);
if(input.compare("END") == 0)
break;
else
command_list.append(input);
}
I tried to use the following code to input strings, find a range, and copy to output
int main(int argc, const char * argv[])
{
vector<string> vec;
copy(istream_iterator<string>(cin), istream_iterator<string>(),back_inserter(vec));
vector<string>::iterator first = find(vec.begin(),vec.end(),"start");
vector<string>::iterator last = find(vec.begin(),vec.end(),"end");
if(first < last)
copy(first,last,ostream_iterator<string>(cout,"\n"));
return 0;
}
I ran the program from unix command line, and terminated input using (ctrl+z), but nothing was written to the terminal. I read other threads on istream_iterator, and I think ctrl+z is the only way to tell the program that I'm done with inputting. But it should output afterwards? (by the way, I did input strings including "start" and "end" to make sure first and last iterators are valid).
I also ran the above program from an IDE (Xcode), but it doesn't show any command line for input, is there any to show it? or do I have to do some
cout << "Please enter words: "
kind of prompt to take input?
thanks!
There is error checking for a missing file or incorrectly typed file name...but for no file_name i.e. the string file_name is empty I get the error
terminate called after throwing an instance of 'std::logic_error'
and then windows throw an error at me as well.
My guess would be to add a check for the empty string... at file_name.str() before creating an instance of type ifstream...but just wanted to check.
void file_to_string(string file_name)
{
string line;
ifstream myfile(file_name.c_str());
if (myfile.is_open())
{
while (myfile.good())
{
getline(myfile, line);
cout << line;
}
myfile.close();
}
else
{
cout << "File : " << file_name << " : did not open";
}
}
int main(int argc, char* argv[])
{
file_to_string(argv[1]);
}
If you don't provide an argument at all, then argv will only contain one string, and argv[1] will be invalid. You'll have to check that before you even try to access it:
int main(int argc, char* argv[])
{
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " filename\n";
return EXIT_FAILURE;
}
file_to_string(argv[1]);
}
There's no particular need to check whether the string is empty; if it is, then the file will fail to open as it would for any other invalid filename, and your code already handles that.
EDIT: ouch - more coffee required. As was clearly stated, this is a string object not a pointer. So, use the string's provided members to verify it has content. In practice, at the top of the file, use string.empty ala:
if (file_name.empty())
{
// bug out
cout << "Invalid filename" ;
return;
}
EDIT2: As Nim correctly points out, this is not bulletproof as there could be no params supplied (and hence your reference to argv[1] will crater). So, check these cases in main() or in another function to verify the params.
You could do something like:
if (argc < 2)
{
// complain about lack of proper arguments
}
else
{
file_to_string(argv[1]);
}
All that said, in a slightly-more involved case you may face the need to add multiple different command line arguments, or for the arguments to be supplied in an arbitrary order (and so you can't rely on the index of '1' like you are here).
Handling all that properly can get fairly complicated quickly, so take a look at the approaches others have already implemented to handle this (and more - what if your options come from a config file instead of the command line?). One such option is Boost's support for program options; see http://www.boost.org/doc/libs/1%5F39%5F0/doc/html/program%5Foptions.html
the error occurs higher than that - what if an argument (i.e. what is in argv[1] is crap!) is not provided? This is why you should look at proper program options utilities...