1

I am working on a project which requires me to open an HTML file and use its contents. I added it to Resource files but when I try to open it lie this:

std::ifstream templateFile;
templateFile.open("filename.html", std::ifstream::in);

The operation fails. I checked it by using templateFile.fail(). The above operation works when I provide the full path. The file lies in the project folder along with other files. I tried setting build action to content but still it doesnt work. Please Help.

tapananand
  • 4,392
  • 2
  • 19
  • 35
  • The file you are opening is not in the current working directory. Either change the CWD or use an absolute or correct relative path. – Captain Obvlious Jul 17 '15 at 15:10
  • @CaptainObvlious: The file is in the project directory with all the other source and header files – tapananand Jul 17 '15 at 15:12
  • @TapanAnand if it is indeed in the same folder as the .vcprojx file (the project folder) and you've done nothing to override the debugging working folder on the `Debugging` project properties, it should work. The default is indeed `$(ProjectDir)` for every flavor I can recall of VS since at least 2003. Of course, some `chdir` buried in your code would make all of that moot, but you've mentioned no such thing in your post. And of course, proper permissions are required (obviously). – WhozCraig Jul 17 '15 at 15:16
  • 1
    Probably should have asked this before. You're running this from the IDE, *right* ? Your usage of the "resource" term in your question subtly suggests you're trying to add the html file to your resource table of your linked image, which is a considerably larger (and substantially more complicated) task to bite off, so my prior comment assumes that is *not* the case. – WhozCraig Jul 17 '15 at 15:22
  • I am running from IDE. I have added it to the resource files folder shown in the Solution Explorer. – tapananand Jul 17 '15 at 15:24
  • @TapanAnand if that comment is related to my prior, that doesn't do much of anything besides provide a handy way for you to open it. It still resides on the file system somewhere, and must still be present in the current working directory of the running process (which as I said, defaults to the same folder where your *running* project's .vcprojx file resides unless you've changed it) for relative/no path open request to succeed. – WhozCraig Jul 17 '15 at 15:27

2 Answers2

1

Output directory, where your executable is compiled and put into differs from the source directory, where you create all your .cpp/.hpp files (I assume there is filename.html file). Local path filename.html is supposed to be local for your executable file, not the source file.

Read more about changing the output directory here: https://msdn.microsoft.com/en-us/library/ms165410.aspx

psliwa
  • 1,094
  • 5
  • 9
  • i tried putting the file in the same directory as the .exe. Still doesnt work. – tapananand Jul 17 '15 at 15:18
  • 1
    @TapanAnand Try to compile your program with added lines to check local directory path (http://stackoverflow.com/questions/4807629/how-do-i-find-the-current-directory) – psliwa Jul 17 '15 at 15:23
  • Local directory path is different from the project directory. How do i deal with this without hard coding the path, so that it ll work even if i just provide the user with the exe? – tapananand Jul 17 '15 at 15:32
  • @TapanAnand It depends whether the `filename.html` is a static file (it's an integral part of your software) or not. If so, you should consider adding it as a resource file. There's an article about it here: https://msdn.microsoft.com/en-us/library/zabda143.aspx – psliwa Jul 17 '15 at 15:38
  • I added it to resource files as an html file but still could not access it. – tapananand Jul 17 '15 at 15:45
  • @PiotrSilwa: How do i access the file after adding it to resources? – tapananand Jul 17 '15 at 16:10
  • @TapanAnand Sorry, you'll have to read Visual Studio's documentation or check out some tutorials because I've never done it in VS. I'm sure it's widely accessible as it's a common feature used by many applications. – psliwa Jul 20 '15 at 10:18
0

Under Configuration Properties / Debugging, see what your Working Directory is using the macros dialog box. Move your file into this folder.

enter image description here

Click the button shown in the figure. There, click either Edit or Browse. Browse will take you to the working directory. Edit will expose the link to open the macros box

The Vivandiere
  • 3,059
  • 3
  • 28
  • 50