fstream odd behavior, very odd!

This one is a head scratcher!
This little block of code works just fine on a Linux X86 Box with gcc7 compiler, but when I use the same program on an Linux ARM7 SBC with gcc9 this fails to check the logs.

1
2
3
4
5
6
7
8
9
10
 // Check to see if log size has changed
	ifstream in1("oldlog");
		string unused;
		while(getline(in1, unused)){
			olog++;
			}
	ifstream in2("newlog");
		while(getline(in2, unused)){
			nlog++;
			}


But,on the same SBC the below works in another program I wrote so ifstream DOES work:
1
2
ifstream file("");    
file.open(input.c_str());


Compile string:

 
g++ -g -Wall -std=c++17 Listener.cpp -lrt -lcrypt -lstdc++fs -lpthread


My first thought was file permissions but I only use the SBC as root so I have permission. I'm at a total loss here and can see no reason unless it's a compiler issue but one would think the newer gcc9 version would be a done deal, any suggestions would be appreciated, Thanks, Tom


Last edited on
Can you write a minimal complete program that reproduces the issue?
> but when I use the same program on an Linux ARM7 SBC with gcc9 this fails to check the logs.
How did you determine this?

And what is 'failure'
- failed to open the file
- failed to make olog anything other than 0
- failed to see any changes in olog

Where is olog/nlog initialised?

There is also no code to check that the files have been opened OK.

If you're just checking that the size of the files has changed - rather than it's content (which the code above doesn't do), then just compare the file sizes:

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

int main()
{
	std::ifstream ifs("test.txt");

	if (!ifs)
		return (std::cout << "Cannot open the file\n"), 1;

	ifs.seekg(0, std::ios_base::end);
	std::cout << "File size is " << ifs.tellg() << '\n';
}


or filesystem::file_size() in c++17.
https://en.cppreference.com/w/cpp/filesystem/file_size


Last edited on
Thank You all for your help, I'll post more code tonight after work, please remember I'm still very noob, also this program is like 200 lines so I only posted the trouble area. I've used the 2 ints to compare file sizes, they increment as those while statements iterate.
I know it isn't working because I did some tests on both computers:

1
2
3
4
5
6
7
8
9
10
11
 // Check to see if log size has changed
	ifstream in1("oldlog");
		string unused;
		while(getline(in1, unused)){
                cout << "testing oldlog" << endl;			olog++;
			}
	ifstream in2("newlog");
		while(getline(in2, unused)){
                cout << "testing newlog" << endl;			nlog++;
			}
Cout << olog <<  " "  << nlog  <<  endl;


I got lots of messages on the x86, and nothing on the SBC. I really have to wonder if this is a compiler issue. I also understand there are undoubtedly better ways to do this and would love to revise it but that still leaves a mystery and since the purpose of these little projects is to gain fluency and knowledge it would be best to figure out the failure in case I see it again.
Last edited on
https://en.cppreference.com/w/cpp/io/basic_ifstream/is_open
Make sure your files are open before trying to read from them.

> I really have to wonder if this is a compiler issue.
It's almost never a compiler issue, when those compilers are used by millions of people every day.
Last night went according to plan, just not mine so I'm just getting back to this....

I wrote that into a smaller program and now it's working on both platforms. I'm going to have to spend more time with it after work tonight.







I found it, I'm an idiot.
Another part of the program I inserted that function into changes the directory.
I had to copy both logs into three different directories and it came to life.
Man was I angry when that test program ran perfect.
> Another part of the program I inserted that function into changes the directory.
And if you'd put the error checking in when you started, or first asked, the problem was solved days ago.

Twice you tried to blame the compiler for a PICNIC problem.
I'm a novice developer, and did implement some error reporting to the best of my ability which ultimately led me to this epiphany.
At the time, I couldn't see the issue so the only differences were platform and compiler version and it seemed plausible there needed to be something added to the compile string.

In all, I'm glad I made this error, user seeplus taught me another way to achieve the goal, so after this all runs I have a new tool to use when I revise the code.

Thank you to everyone who took the time to read this thread, I always spend a couple of days trying to work stuff out on my own but it's a nice feeling to know guidance from good people is a post away if I can't figure something out.
Topic archived. No new replies allowed.