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);
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()) {
returntrue;
}
else {
cout << "The file could not be loaded, please try running the program again" << endl;
returnfalse;
}
}
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
$ cat foo.cpp
#include <iostream>
#include <fstream>
#include <cerrno>
#include <cstring>
usingnamespace 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.
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:
> 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/