fstream fails to open file - c++

I have a very weird problem. The setup is the following:
Library A uses Library B
Both libraries are installed as shared library independently and A links to B using CMake packages.
When I create an executable that directly links to B, everything works fine.
When I create an executable that links to A however, which is using B, then for some reason my fstream in B fails to open a specific file. I am 100% certain the file exists, is not requiring authorization and is not currently used. The error thrown by strerror(errno) is "No such file or directory".
I really have no clue what might go wrong. This is the code snippet I use for opening:
ifstream f;
f.open(filename.c_str(), ios_base::in|ios::binary);
if (f.fail()) {
std::cout << "Opening Vocabulary failed: " << std::strerror(errno) << std::endl;
return false;
}
I triple checked the path in filename is correct. These are the last resorts I can think of:
Maybe Library A uses another C++ Standard as B, which is why the call to fstream fails?
Maybe filename string is somehow corrupted, even though the path seems to be correct?
Maybe some memory leak corrupts my c_str() command?
Is there anything else that I could check?
Edit: Ah, one thing I forgot: The fstream command is in a templated function in a header file. Maybe this has something to do with it?
Edit2: Here is the stack trace. "ORBVoc.bin" is the file I wanted to open. The only information I get out of this is, it does not exist...though it does exist.
https://www.file-upload.net/download-14359408/strace.log.html

Okay, apparently a reboot was all it takes. Today everything works flawlessly. No permissions, file or line of code was changed. I assume either something went terribly wrong, when my Laptop came back from hibernation. Or it was some pending ubuntu update? I really have no idea. I could swear it was shutdown at least once the past 2-3 days. But jeah, I have no other explanation. Maybe someone smarter can answer this with his glass ball...
Anyway, thanks for the answers, appreciate it! At least I learned about strace $:^)

Related

Error with std::filesystem::copy copying a file to another pre-existing directory

See below for the following code, and below that, the error that follows.
std::string source = "C:\\Users\\cambarchian\\Documents\\tested";
std::string destination = "C:\\Users\\cambarchian\\Documents\\tester";
std::filesystem::path sourcepath = source;
std::filesystem::path destpath = destination;
std::filesystem::copy_options::update_existing;
std::filesystem::copy(sourcepath, destpath);
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot copy: File exists [C:\Users\cambarchian\Documents\tested] [C:\Users\cambarchian\Documents\tester]
Tried to use filesystem::copy, along with trying different paths. No luck with anything. Not too much I can write here as the problem is listed above, could be a simple formatting issue. That being said, it worked on my home computer using visual studio 2022, however using VS Code with gcc 11.2 gives me this issue.
Using:
filesystem::copy_file(oldPath, newPath, filesystem::copy_options::overwrite_existing);
The overloads of std::filesystem::copy are documented. You're using the first overload, but want the second:
void copy(from, to) which is equivalent to [overload 2, below] using copy_options::none
void copy(from, to, options)
Writing the statement std::filesystem::copy_options::update_existing; before calling copy doesn't achieve anything at all, whereas passing the option to the correct overload like
std::filesystem::copy(sourcepath, destpath,
std::filesystem::copy_options::update_existing);
should do what you want.
... it worked on my home computer using visual studio 2022 ...
you don't say whether the destination file existed in that case, which is the first thing you should check.
I put the copy_options within the copy function but it didn't work so I started moving it around, I probably should have mentioned that.
Randomly permuting your code isn't a good way of generating clean examples for others to help with.
In the rare event that hacking away at something does fix it, I strongly recommend pausing to figure out why. When you've hacked away at something and it still doesn't work, by all means leave comments to remind yourself what you tried, but the code itself should still be in a good state.
Still doesn't work when I write std::filesystem::copy(sourcepath, destpath, std::filesystem::copy_options::recursive)
Well, that's a different option, isn't it? Were you randomly permuting which copy_options you selected as well?
Trying recursive and update_existing yields the same issue.
The documentation says
The behavior is undefined if there is more than one option in any of the copy_options option group present in options (even in the copy_file group).
so you shouldn't be setting both anyway. There's no benefit to recursively copying a single file, but there may be a benefit to updating or overwriting one. If the destination already exists. Which, according to your error, it does.
Since you do have an error explicitly saying "File exists", you should certainly look at the "options controlling copy_file() when the file already exists" section of the table here.
Visual Studio 2022 fixed the problem

Filesystem cant copy files because they open in another program c++

As you can see by the title I have issues when copying files from a directory to another. The code works perfectly fine but when the files are in use, obviously I get an error. Is there any way I can skip these files? (that are in use) and simply move on to the next?
I have tried looking for a solution to my issue but it seems there isn't a function from what I've seen on the internet for filesystem, any information given helps. I provided the code below for copying files.
std::filesystem::copy("C:\\Users\\"+ user+"\\AppData\\Local\\tee\\test\\User Data", "C:\\Users\\"+user+"\\AppData\\Local\\tee\\test\\Application", std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive);
If you can't copy you will get an exception, if you don't catch that the program chrashes. You can skip the files if you catch the exception with a simple try-catch statement. eg.:
try{
std::filesystem::copy(/*Params*/);
}catch(...){
std::cout<<"cant-copy '"<<filename<<"'";
/*Handle it if you don't want to just ignore (sleep and try later or ask the user what to to...)*/
}

What does "Permission denied" "Id returned 1 exit status" mean?

How do I solve the problem: "Id returned 1 exit status"
#include <stdio.h>
#include<conio.h>
#include<windows.h>
int main()
{
int P, N, NP=0;
printf("Introduzca en nombre del producto:\n");
scanf("%f", &N);
printf("Introduzca en precio del producto:\n");
scanf("%f", &P);
if (P <= 1500)
NP=P*1.11;
else
NP=P*1.08;
printf("El producto %d cuesta %d", NP, N);
getche();
return 0;
}
The full list of errors is:
Permission denied
Id returned 1 exit status
It does not have anything to do with code. Your operating system simply does not allow to modify a file while it is in use, so the compilation (actually, linking, ld is the linker) fails, because compiler can't remove the old executable and place a new one. To solve this, simply close all existing processes running that program.
If that won't work, check your permissions for directory the executable is in, or look for any programs that are currently using it (some systems allow programs to place a lock on a file, so no other program can modify it).
Deleting the executable(.exe) file solved the problem. If you are compiling the code multiple times then it may not able to replace the old .exe file.
There's something wrong here:
int N;
scanf("%f", &N);
Your data types do not match.
I know the problem is already well solved, but I want to offer another perspective for those who are haunted by the bug message
"[Error] Id returned 1 exit status"
So here it is:
If you compile a C/C++ source file with no main function to execute,
there will definitely be a bug message saying:
"[Error] Id returned 1 exit status"
But sometimes we just don't need main function in the file,
in such a case, just ignore the bug message.
Alt+F2 in your ide... its sometimes realy helps when you forgeting to close "run" window hah)
This happened to me when i accidently marked my .exe file read only. When i removed it it worked again. (IDK much about programming but this happened to me mayb it helps someone)
if you are compiling in the terminal you need:
su
gcc file.c -o nameyouwant
./nameyouwant
I'd like to add something to papeho's answer:
If you are using any antivirus software, this may be the reason.
Some antivirus might be the fact here, probably for including stdio.h,windows.h or conio.h it predicts that working with such headers may causes attacks on your PC
Probable ways to get rid of it:
1.Try to delete the existing executable file. (Failed because it's being locked on something (maybe antivirus) )
2.Simply rename that file, then rebuild and done (renaming will allow to create another executable file for your code).
3.If failed restarting pc and trying from 1 may fix this.
Hope it helps :-)
I was having the same error. And it was a bit difficult to solve because the message says that don't have permission to open the file. But the problem is that the program is still running.
If the program is still running I thought that I will find inside task manager, but wasn't there. So, I tried to delete the file and I wasn't able to delete too. Searching more about I find the program "Process Explorer". With this program I was able to see that my exe file is still running, but with a message saying that the program was suspended. Then I tried to kill through Process Explorer, but without success, because I still don't have permissions. So I close Process Explorer and open with administrator permissions and after all this I was able to kill the process and build and run my file through codeblocks.
I think you should simply change int NP=0; to: float NP=0;
Also change: scanf("%f",&N); and: scanf("%f",&P);
to: scanf("%d"&N);and scanf("%d"&P);
And:getche(); to: getch();
I hope this can help you.
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
void gotoxy(short x, short y);
int main()
{
FILE *ptrFile= fopen("E:\\ENCD_OUTPUT.html","a");
char a[30];
cin >> a;
cout << a;
gotoxy(14, 4);
fputs(a, ptrFile);
gotoxy(13, 6);
fputs(a, ptrFile);
getche();
}
maybe you forgot to close the terminal window, simply close it and run again.
The following two worked for me:
Delete the .exe file and compile again
Check if the code is not already running in the terminal (to make sure of this, just close and re-open your IDE/editor)
I am facing the same problem, no matter what, it kept saying the same thing("Permission denied" "Id returned 1 exit status"). I tried many things and this one worked for me.
Go this location [C:\MinGW\mingw32\bin ] and delete the Id.exe file then create new Id.exe file in the same directory.it worked for me.
How to create Id.exe file ??
Just create a empty text document in the above shown directory and type Id.exe as its file name.
NOTE:Double check if you saved the program or not if you are using the vs code. Sometimes it shows error for not saving the program.
Check first that are you using flushall(); or clrscr(); in your program if it is there then for sure you will get that error!!! as it is not allowed in Dev CPP. Use fflush(stdin); instead of it.
The problem will be solved surely!!!

Boomerang decompiler fails

I need to decompile a windows program which the source code was lost for a long time.
I am using boomerang in Windows 7 for this. However, it looks broken, gives this message and quits:
Could not open dynamic loader library Win32BinaryFile.dll (error #998)
Googling about it gives no useful results. Looking in the boomerang source code, it is apparently coming from this:
00137 hModule = LoadLibraryA(libName.c_str());
00138 if(hModule == NULL) {
00139 int err = GetLastError();
00140 fprintf( stderr, "Could not open dynamic loader library %s (error #%d)\n", libName.c_str(), err);
00141 fclose(f);
00142 return NULL;
00143 }
I.e. LoadLibraryA is failing with the status 998.
What could I do to fix that?
Edit, four hours later:
The program that I want to decompile is a work that me and a friend implemented in 2005. The source just gone in the mean time without we seeing that. Now, in 2013, when we searched it, nothing was found. In retrospect, it was probably lost in 2008 or in 2010, two occasions where my computer hardware crashed and I needed to get a new computer (and lost a lot of data with that). We had several backups scattered in several places, but after an exhaustive search, I found nothing.
I know that since boomerang is open source, I could just get its source code and hack it around. However, that sort of task is not what I originally intended to do, since the focus is just to decompile my program and I guess that I am missing something simple, since it can't load the DLL while it is clearly there.
I don't need the exact code back, just a sketch of what were the exact details of the algorithm that were implemented. Having that, I can rewrite the rest again.

Access Violation exception when running a release-built application

Recently I have been developing a small OpenGL game. Everything in it runs fine with the debug build but when I build the release, I get a strange Access Violation exception.
I searched across the code and it seems that the problem occurs when I try to open a file. Here is the function where I think the problem is coming from:
#define LOCAL_FILE_DIR "data\\"
#define GLOBAL_FILE_DIR "..\\data\\"
std::string FindFile(const std::string &baseName)
{
std::string fileName = LOCAL_FILE_DIR + baseName;
std::ifstream testFile(fileName.c_str()); // The code breaks here
if(testFile.is_open())
return fileName;
fileName = GLOBAL_FILE_DIR + baseName;
testFile.open(fileName.c_str());
if(testFile.is_open())
return fileName;
throw std::runtime_error("Could not find the file " + baseName);
}
This code is associated with loading of GLSL shaders. A function takes the shader's file name and then passes it to FindFile in order to find the file needed.
Just as a general rule from personal (and teaching) experience: >90% of the cases where Debug works fine and Release crashes are due to uninitialized variables. That's a little harder to do in C++ than in C, but it is a very common problem. Make sure all your vars (like baseName) are initialized before using them.
I fixed the problem.
Everything was happening because I have made the Release build using glsdk's Debug build libraries. Changing to the Release build libraries fixed the problem.
Check that baseName is valid. Try printing it out. You may be getting a corrupted copy of baseName or your stack may have gotten trashed prior to that point (same result).