arranging variables to be read

If I do this:
ex. 1
1
2
3
4
5
6
7
8
9
10
11
12
void load_custom()
{
	string path = "customOpenCVsettings.txt";         
	ifstream fin;

	fin.open(path);                        // Open the file
	if (fin.is_open())                      // If it opened successfully
	{
            fin>> var1 >> var2 >> var3>> var4;
            std::cout << "open custom file a success" << std::endl;
	    fin.close();                   // Close the file
	}


If I "line them up", like ex.1 its not a problem. If I do it like ex.2, line after line, an error is thrown and window is closed usually is I list more than 3 going down.

What's really going on here and how do I load them like the second example, if possible.
ex. 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void load_custom()
{
	string path = "customOpenCVsettings.txt";         
	ifstream fin;

	fin.open(path);                        // Open the file
	if (fin.is_open())                      // If it opened successfully
	{
            fin>> var1; 
            fin>> var2;
            fin>> var3;
            fin>> var4;
            std::cout << "open custom file a success" << std::endl;
	    fin.close();                   // Close the file
	}
Last edited on
If I do it like ex.2, line after line, an error is thrown and window is closed

What is the exact error message?

Both methods are really the same so there must be something else wrong that you're not showing.

In addition to what @jlb wrote...

Your output in line 10 (second example: 13) should probably go before line 9. Your message states that you opened the file successfully, and you know that immediately in line 8.

The reads may or may not have succeeded. You can check fin.good() after every read to see if the stream is still in a good state (the read was successful). So, you could print that the read was / was not successful at the locations you show if you want to.

Note: where you have the "open success" prints is technically correct. If you got there, the file did open correctly. But the stream state could already be corrupted.
good call
I've got a corrupted stream (it appears that way). Its been playing havoc with the programming trying to isolate whats wrong. As stated earlier it will function fine in the first several reads but resort to unusual behavior later, inserting values where there are ones, zeros where there should be >0.

Data:
my bools:
1
1
1
1
1
1
1
0
0
0

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::cout << "Default file opened" << std::endl;
		fin >> params.thresholdStep;
		std::cout << "Params" << params.thresholdStep << std::endl;
		std::cout << fin.good() << endl;
		fin >> params.minRepeatability;
		std::cout << fin.good() << endl;
		fin >> params.minDistBetweenBlobs;
		std::cout << fin.good() << endl;
		fin >> params.minThreshold;
		std::cout << fin.good() << endl;
		fin >> params.maxThreshold; 
		std::cout << fin.good() << endl;
		fin >> params.filterByColor;
		std::cout << fin.good() << endl;
		fin >> params.blobColor;
		std::cout << fin.good() << endl;
		fin >> params.filterByArea;
		std::cout << fin.good() << endl;
		fin >> params.minArea;
		std::cout << fin.good() << endl;
		fin >> var;
		std::cout << fin.good() << endl;


How do I address this?
You're not showing enough content.

Post the smallest possible complete program that illustrates the problem, don't forget to post a small sample of your input file as well. At very minimum we need to see how all the variables are defined and the sample input file.

One way to see a possible cause of this type of problem is read all the data as a string, printing each string on it's own line.

Remember if you have some piece of data that can not be held in the type of variable you're using the stream will error and no more input can be read until the error is cleared.

So what is the type of the params.filterByArea variable and what are you trying to cram into this variable?

Thank you

Implementing your advice to error check the input settings file(.txt) I came across a deliberate space at position #7 for params.blobColor. I didn't know what else to put in the file at the time. I changed position 7 to a zero and like unlocking a key all of the bools went to 1, flagged vars put out the right value!

 
fin >> params.blobColor;  //was set to "" changed to 0 


result: my bools
1
2
3
4
5
6
7
8
9
10
1
1
1
1
1
1
1
1
1
1
Topic archived. No new replies allowed.