0

I'm trying to access the length of the compiled binary of a program, but it returns -1. Could someone point me on the right track? I'm uncertain as to why the following code isn't producing the correct result.

std::fstream file(argv[0], std::ios::binary | std::ios::ate); std::cout << file.tellg() << "\n"; 
2

2 Answers 2

1

Simply adding std::ios::in to the open mode flags makes it work for me. (The constructor was failing to open the file. According to the Standard, you must specify one of in, out, or app.)

Changing the stream type to std::istream also works, but the resulting binary is 8 bytes larger.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, thanks so much. Changing the stream type actually broke it for me, but with the ios::in specification it worked fine. Odd that istream would increase filesize by so much though.
1

A result of -1 indicates that the open failed. You should always test for this:

if (std::fstream file(argv[0], std::ios::binary | std::ios::ate)) { std::cout << file.tellg() << "\n"; } else { // Report error. } 

The second problem is that if you just want to get its length, you should open it for reading only (this might be why the open is failing):

std::ifstream file(argv[0], …); 

The third issue is that argv[0] isn't guaranteed to contain a valid executable name. That's just a widely held assumption. You'll usually get away with it, but you should keep it in mind.

8 Comments

Ah, thanks. I'll add in a check to make sure it reads the executable, didn't realize it wasn't a guarantee.
Related to this problem is that, even if argv[0] contains a meaningful filename, it might still not resolve correctly when passed to ifstream. For instance, if your program foobar is found on the path and isn't in the current directory, argv[0] will contain foobar, which will fail to open because there is nothing called foobar in the current directory.
… or if the user executes the program with different permissions from what the shell used to find it, such as with sudo someuser. There are countless things to go wrong. C++ doesn't even guarantee there's a filesystem on the machine at all. And of course the program might not be stored in a file, even if there is a filesystem.
@user2142343: Yes, but if you took my advice to use std::ifstream, then you've essentially stipulate that you only want to read. Even if you fix this, you may not be permitted to write to the binary of a running program. You certainly can't in Windows; Linux is less restrictive, but you may still encounter problems.
@user2142343: You'll probably get a much more helpful response from SO if you provide some background information. Why do want to do this? What are you trying to achieve? This will reduce the risk of stumbling over the XY problem.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.