Input Through Command Prompt

I am currently reading an input file into my code using ifstream input_file("data.txt") where data.txt is the name of the input file. I want the input file to be able to be read through the command prompt. Can I just change the code to ifstream input_file(argv[1]) ???

Also is it ok for this to not happen in the main function and can it read the input file multiple times throughout the code?
Last edited on
Help Please! Should be a quick one for anyone who has worked through the command prompt.
No one has any information on this stuff?
ifstream input_file(argv[1]) This is fine as long as the user gave an argument. If you want to make this inside another function you can pass the string to the function as an argument of type const char* (or use std::string). You can read the file multiple times just fine.
1
2
3
4
5
6
7
8
9
ifstream input_file(argv[1]);
if(!input_file){
cout << "The file could not be opened. Please try running again from the command prompt, and remember to specify a filename." << endl;
cout << "Press the 'ENTER' key to close the program.";
cin.ignore();
cin.get();
return 0;}
// stuff
input_file.close();


Just in case they didn't run it from the command prompt...
Last edited on
I am having trouble passing it to the other function at this point. Say the input file is data.txt . It is entered using the command prompt as expected. How do I pass this to a function and what if I call the function in a function other than main. for instance I have 2 functions get_data1() and get_data() and then main(int argc, char* argv[]) . I need to pass argv[1] through get_data1() from get_data(). This is done without going through main.
Like I can't just pass argv[1] through the function because I am calling the function outside of my main function where argv[1] has lost scope.
¿did you ever tried? argv will continue to exists until your program terminates.
If you still have issues, show some code.

Edit: I think that I understand now. 'argv' is not global, so you need to pass it to the wrapper.
Last edited on
yea it isn't global. I am not sure how to pass it though because I am not calling the function in main. I am calling the function from a different function so I don't know how to pass argv through that...
You can pass it to a function which pass it to a function which pass it to a function ...

Or you can have a global const char* variable that you assign to argv in main. I don't saying it's a good way, but it's possible.
Pass it how? What is the correct function setup? Like lets say we have three functions 1, 2 and main.

We have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

vector<vector<double>> 1(){

//code here that uses argv[1]

return 1

}

vector<vector<double>> 2(){

//code here that uses 1() in the form of vector<vector<double>> Intermediate = 1()
//This function is used many, many times throughout other functions in the code along with in the main function.

return 2
}

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

//many functions and outputs

}
Last edited on
Okay, so you need to use 'argv' in another function. I would make a global variable, and set its value to 'argv'.

1
2
3
4
5
6
7
8
9
10
void some_function(); 

int main (int argc, char* argv[]) {
char* hold_value[] = argv;
// do things that the program likes to do.
}

void some_function() {
// blah blah blah
}
Last edited on
so if i did that then I could pass argv[1] through any function at any time and it would work?

I am not sure if you guys realize yet but I am a very competent programmer with no f"in clue what argv[] is and isn't able to do.

Aka I am a horrible programmer with a few weapons, none of which conform to the task I NEED to have completed by two weeks ago... f..uck....
OK so the real issue I have with your response (the issue only comes up because I have to submit this to my prof) I set a hold_value variable equal to my argv[1] but that variable LOSES SCOPE the second I leave my main function, right? If not then great, that makes life easier but I don't understand how I can call 1() without inputing any variable and still get the correct return. This should not be this hard considering this is a language that is so simple.

However, because of the lose of scope I am litterally not helped at all by your suggestion. The input file (arv[1]) still cannot be passed through my 1() function and the 2() function that actually deals with the 1() function directly still has no ability to interact with 1().......
Never call argv[1] directly without checking argc first. If your users call your program without arguments, you will get most likely a crash.
globals are evil, so use them with care.
1
2
3
4
5
6
const char *file_name = NULL;

int main(int argc, char **argv){
  //...
  file_name=argv[1];
}
1
2
3
4
5
6
7
8
9
void foo(const char *file_name){
  //...
  bar(file_name);
}

int main(int argc, char **argv){
  //...
  foo(argv[1]);
}
You could also pass the stream.
gh24 wrote:
read the input file multiple times throughout the code?
¿does the file change while you are runnning the program? If not then parse the file just one time.

If you could be more precise will may help you better. Don't tell us what you are doing, but what do you want to accomplish.
I want to read a data file and make a vector of vectors with that data file. The name of the data file will be inputted through the command prompt. Here is the code that is working with the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

vector < vector<double> > get_data1(){					

	double value_just_read_from_line;
	vector< vector<double> > D(0);
	int lineCount = -1;
	string line;
	int lengthOfLineTerminator = 1;

	ifstream input_file ("Data.txt");					

	if (input_file.is_open ())
	{
		input_file >> lineCount;
		input_file.ignore(lengthOfLineTerminator);

		int index = 0;
		while (!input_file.eof()) {						

			getline(input_file, line);					

			vector<double> v;													

			while (stream >> value_just_read_from_line) {
				v.push_back(value_just_read_from_line);								
					
				if (input_file.peek() == '\r' || input_file.peek() == '\n')
					break;
			}
			D.push_back(v);															

			index++;																

		}
	}
	else
			cout << "Input_file_Data.txt_does_not_exist_in_PWD" << endl;

	return(D);																		

}

vector <vector<double>> get_data(){
	vector < vector <double>> P;
	vector<vector<double>>D;
	vector<double> s;
	D = get_data1();
	for(size_t j=0; j<D.size(); j++){
		vector<double> v = D[j];
		for( size_t i = 0; i < 2; i++){
			s.push_back(v[i]);
		}
		s.push_back(-D[j][0]);

		for(size_t t=2; t<v.size(); t++){
			s.push_back(v[t]);
		}
		P.push_back(s);
		s.clear();
	}
	return(P);
}


Currently I have the file name in the code already but I need to change that. I am just not sure how to pass the file name from the main function to the other two functions I have above.
Last edited on
Topic archived. No new replies allowed.