Program cannot open file in another distro

Hello. I decided to try to run a program that I did in another Linux distro.
It runs, but at a certain point it prints a warning message that I putted there:
The file could not be loaded, please try running the program again


The line that I believe to not be working it is:
TGAManager tgaManager ("z.tga",true);

whose constructor definition it is:

1
2
3
4
TGAManager::TGAManager(const char * filename, bool choice) : FileManager (filename, choice) {
    TGAheader[0] = 0; TGAheader[1] = 0; TGAheader[2] = 2; TGAheader[3] = 0; TGAheader[4] = 0; TGAheader[5] = 0; TGAheader[6] = 0;
    TGAheader[7] = 0; TGAheader[8] = 0; TGAheader[9] = 0; TGAheader[10] = 0; TGAheader[11] = 0;
}


and the FileManager constructor it is:

1
2
3
4
5
6
FileManager::FileManager (const char * name, bool choice) {
   if (choice)
      file_writer.open (name,ios::binary);
   else
      file_reader.open (name,ios::binary);
}


file_reader and file_writer are
1
2
3
protected:
    ofstream file_writer;
    ifstream file_reader;


after TGAManager tgaManager ("z.tga",true); it comes:
if (tgaManager.isFileOpen()) {

1
2
3
4
5
6
7
8
9
bool FileManager::isFileOpen () {
    if (file_reader.is_open() || file_writer.is_open()) {
      return true;
    }
    else {
      cout << "The file could not be loaded, please try running the program again" << endl;
      return false;
    }
}
that finally prints the error message and then takes the program to its end.

So, what in the above code might not work in one distro and works in another. I really have no idea, neither experience in running software in different OSs.
Thanks for the help
I have to ask....is z.tga there, and is it all lower case?
Try constructing a minimal example, like so.
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
$ cat foo.cpp
#include <iostream>
#include <fstream>
#include <cerrno>
#include <cstring>
using namespace std;

int main()
{
    ofstream file_writer;
    ifstream file_reader;

    // Because you have TGAManager tgaManager ("z.tga",true);
    file_writer.open ("z.tga",ios::binary);

    if (file_reader.is_open() || file_writer.is_open()) {
      cout << "The file is open (well one of them is)" << endl;
    }
    else {
      cout << "The file could not be loaded, please try running the program again" << endl;
      cout << strerror(errno) << endl;
    }

    return 0;
}

$ g++ foo.cpp
$ ./a.out 
The file is open (well one of them is)
$ ./a.out 
The file is open (well one of them is)
$ chmod a-w z.tga 
$ ./a.out 
The file could not be loaded, please try running the program again
Permission denied


Rather than just printing a useless "that didn't work" message, add some code that attempts to diagnose the reason for the failure.

Maybe you don't have write permission on the 'current directory' that you're in.

Maybe you're not even in the 'current directory' that you think you're in.
I have to ask....is z.tga there, and is it all lower case?


No, but it shouldn't be, as it has to be created.

Maybe you don't have write permission on the 'current directory' that you're in.


That's it.

Rather than just printing a useless "that didn't work" message, add some code that attempts to diagnose the reason for the failure


That seems to require Linux/Unix specific headers so I can check for the writing permissions, right?

I am willing to do so, if you know a good link, feel free to show me.

If does not require Linux/Unix, how I would do so?
salem c's example prints strerror(errno), which in this case would say "Permission denied". If you want to check for write permission before trying to open the file for writing... can you use C++17? If so, the filesystem library has something you might be interested in:

https://en.cppreference.com/w/cpp/filesystem/file_status
https://en.cppreference.com/w/cpp/filesystem/perms

-Albatross
> That seems to require Linux/Unix specific headers so I can check for the writing permissions, right?
No, cerrno and cstring are the C++ wrappers for the standard C header files errno.h and string.h respectively.
http://www.cplusplus.com/reference/clibrary/
Registered users can post here. Sign in or register to post.