input files using command line arguments possible?
I'm trying to test an input(which is a text file) on my code but instead of using file-I/O I added command line arguments to main, initial lines are like these:
Code:
int main(int argc, char** argv){
int n = atoi(argv[1]);
int* values = malloc(n* sizeof(int));
for(int i=2; i<n; i++){
values[i-2]=atoi(argv[i]);
}
//...rest of code
and then compiled and called the program from terminal like this:
Code:
cat input4.txt | ./best_index_cli
//(best index is the name of the problem).
but it gives a "segmentation fault(core dumped)" error. This error happens when we try to access some part of memory that doesn't belong to our program, but this time I don't think problem is with my code. I've tested the code already without using CL arguments (using scanf) and it works fine. So maybe the terminal command I have used is "impossible"??
(I also have question about optimizing my code since it passes 10 test cases but fails on other 2 other ones, but I didn't want to put two questions in one thread and make it unreadable, if someone can help me I'll post it in the comments.)