I have a program and that needs to read from a cin command in the terminal in linux.
What i do is that i start my program like this
./sort <input.txt
and now my program read the contents in given textfile and put it into a vector.
But if i for some reason forget to add the cin command and only write
./sort
the program crashes and i need to press ctrl+c in order to terminate the program. I was experimenting on a couple of solutions but i did not manage to solve it.
My function from reading from the text file and putting the contents in a vector can be seen here. One thing i tried as you can see is adding a if-statemnet with (input.fail()) but that did not work. I also tried cin.fail() which did not work either. Any other suggestions? Thanks
If you mean that you are giving command line arguments, then you can just check the value of argc. If its less than 2, then the file was not passed to the program, and you can then terminate the program.
The previous suggestion to use command-line argument argv[1] may be more suitable, but you will need to change your program to handle that, rather than using the standard input (cin).
NINJAEDIT: Did not seee Chervils reply.
So i have to rewrite the function to do it? I am not sure if i have time for that, this is a assignment due tomorrow and i finished but this problem is annoying me so much i wanted to solve before i turned my program in.
If there is no other way the rewriting the function i think it will have to do like this.