Global Variable From Command Prompt

Hi guys,

I am trying to create a global variable that is read from the command prompt. I have no clue how to do this though.

Can I just do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <complex>
#include <lp_lib.h>


using namespace std;
Char* input_file = ifstream input_file (argv[1]);

**functions**


Also if that does work and creates a global variable input_file how do I get that to then read through the following function when it is called?
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
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 code					

	if (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.

Any help is greatly appreciated!!
Last edited on
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, const char* 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.
The input file is of the form:

3
1131.27 10 67 67 67 67 67 67 67 67 67 1067
1121.39 11 70 70 70 70 70 70 70 70 70 70 1070
1148.75 12 75 75 75 75 75 75 75 75 75 75 75 1075
1790.85 10

and is just a .txt file.

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?

I have, for example:

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
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, const char* 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, const char* 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <fstream>

void get_data1(const std::string& filename)
{
	
	std::ifstream input_file(filename.c_str());
	// ...

	// return ...
}

void other_function1(const std::string& filename)
{
	get_data1(filename);
}

int main(int argc, const char* argv[])
{
	if (argc >= 1)
	{
		other_function1(argv[1]);
	}
}


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.
Last edited on
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 
So if I have a global variable as follows:

1
2
3
4
5
6
7
8
9
10
11
#include <fstream>

std::ifstream input_file;

int main(int argc, const char* argv[])
{
	if (argc >= 1)
	{
		input_file.open(argv[1]);
	}
}


I can just call get_data1() as follows and it will work?:

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
vector < vector<double> > get_data1(){													

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

	if (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);																		
}


where I don't even have to call the input_file in the function?
Last edited on
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.
So where do I put in the clear part? At the end of get_data1() after the return?
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.
Last edited on
Ok cool.

Just so I understand this let me paraphrase:

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.

Is that anywhere close to right?
Topic archived. No new replies allowed.