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");
//Data.txt is the name of the input file that I am trying to read from the command
//prompt instead of having the input file written into the codeif (input_file.is_open ())
{
input_file >> lineCount;
input_file.ignore(lengthOfLineTerminator);
int index = 0;
while (!input_file.eof()) {
getline(input_file, line);
stringstream stream(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);
}
I know global variables are frowned upon but that is the only way I know to make this work. I need to have that input file read through the get_data1() function many times in functions throughout my code that are called outside of main.
command line arguments are passed in as arguments to main
1 2 3 4 5 6 7 8 9
#include <iostream>
int main(int argc, constchar* argv[])
{
for (int i = 0; i < argc; i++)
{
std::cout << argv[i] << std::endl;
}
}
I didn't get if you wanted input_file to be a string or stream. In both cases it is possible to pass them as arguments to functions to avoid having them global.
I know how to read it from main but I have like 12 functions in the code and they call the get_data1() function as well. Doesn't it lose scope in that instance?
vector<vector<double>> get_data1(){
//only place where the input file is actually read and written to D
}
vector<vector<double>> get_data(){
//adjust vector D by calling get_data1() in the form of
vector<vector<double>> D;
D=get_data();
//Use D to make another vector<vector<double>> P that is worked with throughout in other functions
}
float other_function1(){
P = get_data();
//Use P to get some value
}
float second_other_unique_function(){
P=get_data();
//do whatever with P
}
int main(int argc, char* argv[]){
P=get_data();
//Use P over and over again
//Do ALL the things that use other functions and their return values in different ways
}
It is important to note that I only use D in the P function but I call the P function a lot. I may not need a global variable but from my minimal understanding of C++ I will lose scope on my argv[1] input file outside of main and will thus be unable to use P all over the place as I am right now.
I know global variables can be simpler for small programs and I don't want to make things unnecessary complicated for you so I will first show how to use global variables and then how to pass arguments instead.
If you want to store the file name as a global variable you can define a global string variable and assign to it in main.
1 2 3 4 5 6 7 8 9 10 11
#include <string>
std::string filename;
int main(int argc, constchar* argv[])
{
if (argc >= 1)
{
filename = argv[1];
}
}
If you want to have a global ifstream you can create a global ifstream variable and open it in main.
1 2 3 4 5 6 7 8 9 10 11
#include <fstream>
std::ifstream input_file;
int main(int argc, constchar* argv[])
{
if (argc >= 1)
{
input_file.open(argv[1]);
}
}
If you want to avoid global variables you can make the functions that needs to know about the variable take it as an argument.
Note1: const std::string& filename is used here to avoid creating copies of the string objects each time it is passed to a function. If you prefer you can pass it as std::string filename. That will work too.
Note2: If you want to pass a std::ifstream object you should pass it as a non-const reference std::ifstream& input_file. The reason for this is that you can't make copies of stream objects so it has to be a reference. When you read from the stream object you will change the stream state so it will not work if you use a const reference.
So I would rather just use the global variable simply because there are like 15 other functions that I do not want to be in my main function.
My other question is how do I get this to work or flow into the get_data1() function?
As I currently have it setup I need to have ifstream input_file("file name") first in order for it to read through the file don't I? I might just be completely missing how this stuff works but I don't understand how to read the input file without having it being "opened" or initialized or whatever in the get_data1() function.
I guess my real confusion at this point is how I make the string pass through the get_data1() function if it is a global variable.
So I would rather just use the global variable simply because there are like 15 other functions that I do not want to be in my main function.
Passing arguments to functions doesn't necessary change how many functions you call from main. In the example I didn't call get_data1 from main but it was still possible to pass the string to that function.
A global variable is available everywhere. just use it.
In case you have a global ifstream object you might want to reset the stream to start reading from the beginning again.
1 2
input_file.clear(); // clear any error flags
input_file.seekg(0, std::ios::beg); // start read from the start of the file
That should work, but note what I said about restoring the stream. If you call get_data1() twice as it is now the second call will fail to read from the file because the file pointer is pointing to the end of the file already.
Not after the return because that code will never run. Put it at the end before return or at the start of the function. You might have other functions using input_file so it is probably best to put it at the start of the function to make sure you start from the correct position.
I am opening the file in main and setting it as the global variable input_file. When get_data1() is called then the global variable is recognized in the if statement at the top and the function is run creating the output D. If it is called again then I use the clear statement to be sure that I am reading the file from the beginning again and everyone is happy because it works.