0

When I run the Debug in Visual Studio for a project, fopen function works fine. It tries to open a file contained in the project and that I added in the filter "Resources". But when I run .EXE file of my project, I get the null pointer exception: 0x000005c.

When I added the file to be in the same directory as my .EXE file, the exception disappeared.

This is the instruction I use :

fopen(&filename, "rb");

I know it is adviced to use fopen_s instead, but the file is not found anyway...

Apparently, the file is searched always in the current directory...

So, how to include the file in .EXE and make the path of the file relative to the .EXE, at a way it will be contained in the .EXE and not added to the directory where there is .EXE?

Farah
  • 2,469
  • 5
  • 31
  • 52
  • Why dont you try to build your application in `Release` mode – A B Apr 22 '14 at 15:17
  • Hello sir. I built it in Release mode. – Farah Apr 22 '14 at 15:19
  • Is your file is in same location of exe – A B Apr 22 '14 at 15:19
  • When my file and the .Exe in the same directory, it works. But when my file contained the project, it doesn't work, except while debugging. – Farah Apr 22 '14 at 15:22
  • fopen() can only open files, not resources embedded in the executable file. Use the winapi to get a pointer to the resource data, equivalent to fread(). LoadResource + LockResource, SizeOfResource if you need the length. – Hans Passant Apr 22 '14 at 16:20
  • @HansPassant, Can you help me with a sample code to get your idea? Thank you a lot! – Farah Apr 22 '14 at 17:08
  • possible duplicate of [Load resource as byte array programmaticaly in C++](http://stackoverflow.com/questions/16527973/load-resource-as-byte-array-programmaticaly-in-c) – Hans Passant Apr 22 '14 at 17:11

1 Answers1

1

You can't include the file in the .exe. You just need to make sure that the file is in the same directory as the .exe.

If you really, really want to only use one file, you could either:

  • Zip the .exe and the text file together and make sure you include in a readme that the text file needs to be in the same location as the .exe

  • Use an array/struct/some other way of storing the contents of the file in the program itself, and reference that instead of using a file (I assume you don't care about users being able to edit this data outside of an instance of the program since you wanted it bundled with an executable, so the file is unnecessary in that case)

The reason the program only works when you put the file in the directory of the .exe is because the path to the file is defined in the program as something like .\file.txt or file.txt. When the file isn't in the same directory as the .exe, the program will try to find it, and will be unable to, which is why you get the error. It works when you debug because you have a copy of the text file in the same location as the debug .exe.

EDIT: I would also ignore the warnings about fopen_s and other variant's that append a _s to the end of a command - these are windows specific, non-standard, and in most cases, pointless. If you know this program will only be used in windows environments and you're not doing something for school where you are required to write standard code, I suggest you use the _s variants for the sake of security, but it will reduce portability of your code.

ozzymado
  • 960
  • 3
  • 15
  • 26
  • The problem is that the file are critical... And so I need them to be contained the .EXE, and I would apply protection to this .EXE by Themida. So I find it risky to put the file outside the .EXE .. – Farah Apr 22 '14 at 15:25
  • Then go with the second option and don't use a file. Store the contents of the file in an array (if the data is of one type) or a struct array (if the data is of multiple types). If you want the data to persist across multiple instances, change the path to something specific - e.g `C:\myProgram\data.txt` so that the program will create the directory if it doesn't already exist, create the text file if it doesn't already exist, and will require no additional effort on the user's part to make sure it works. You can then encrypt the contents in the text file to insure security as well. – ozzymado Apr 22 '14 at 15:28
  • If you're looking for such a level of security that even an encrypted text file isn't secure enough, you're going to need to use something other than a text file (external database, for example) – ozzymado Apr 22 '14 at 15:32