0

While following along the vulkan-tutorial I've encountered a problem. I am simply unable to open files using the method he uses.

I have a file called shader.frag.spv, which is fragment shader compiled to spir-v code. It is in my source folder, where my readFile is as well.

Here is the code that reproduces my problem:

#include <iostream>
#include <fstream>

void readFile(const std::string& filename) {
    std::ifstream file(filename, std::ios::ate | std::ios::binary);

    if (file.is_open())
        std::cout << "test";
    else
    {
        std::cin.get(); // I always land on the else block
    }
}

int main()
{
    readFile("shader.frag.spv");
}

Restarting visual studio, changing the name of the file, altering its content, moving it to separate folder, using an absolute path. None of this has solved my issue.

Any ideas?

Ab Majeed
  • 106
  • 4
Cortex0101
  • 831
  • 2
  • 12
  • 28
  • What is the purpose of passing `std::ios::ate`? It makes no sense for an input-only stream. – Some programmer dude Dec 06 '18 at 08:12
  • 1
    As for your problem, are you sure that the file is in the "current directory" of your program? The default "current directory" in Visual Studio is the project root, not where the executable file is. – Some programmer dude Dec 06 '18 at 08:13
  • Its out of context here but in the tutorial the advantage of starting to read at the end of the file is that we can use the read position to determine the size of the file and allocate a buffer. And yes that was indeed my problem. – Cortex0101 Dec 06 '18 at 08:17
  • 1
    This is such a common thing with VS that has tripped numerous developers, but until they fix the dreaded project properties window for C++ and make it clearer, I doubt it'll improve. As a side note, you can explicitly set the CWD using project options to point it to whenever the binary is. Following the VS model isn't that dumb, though, because then your shaders can reside within the source structure, and both debug and release builds will read them just the same. – Bartek Banachewicz Dec 06 '18 at 08:31
  • Your code works well the problem is with your file: https://onlinegdb.com/SJowbPUyV I suggest that you have to move your file to another directory. – I_Al-thamary Dec 06 '18 at 08:45

1 Answers1

1

The code should work, given that the file is in your current working directory already, check that!

By default current directory in Visual Studio, is the project root, not where the executable file is, as @someProgrammerDude mentioned.

If you are not sure about it, check C++ Visual Studio Current Working Directory.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Im a newbie if you couldent tell, but theres my answer, I assumed the current working directory would be where my main function was located. Ty! – Cortex0101 Dec 06 '18 at 08:18