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?
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.
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...
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.
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...
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
}
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
}
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().......
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:
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.