Ifstream not opening

I am using Visual Studio 2013

So, I need to open the .ppm image file and work on it a bit, but ifstream, which I am trying to use to read data, cannot open the image file. I am sure that the image file is in the working directory (I created and read some simple .txt files to make sure). And even after extensive research, I cannot understand what is happening.

Here is the corresponding code

EDIT: I added some more code to understand what I'm trying to do

Image * PPMImageReader::read(std::string filename){

        std::string line;
        int width, height, max_val;


        std::ifstream src(filename, std::ios_base::binary);



        if (src.fail()) { //failbit is always set but not badbit
            perror("Logical error on i/o operation. failbit was set\n");
            if (src.bad())
                perror("Read/writing error on i/o operation. badbit was set");
        } 
        if (!src.is_open()) { //and of course this return true
            printf("File was not opened\n");
            exit(1);
        }

     //Edited

     getline(src, line, '\n');

        if (line.empty())
            getline(src, line);

        if (line.find("P6") == std::string::npos) {
            printf("wrong format\n");
            exit(1);
        } 
+4
source share
1 answer

As already mentioned, the problem is related to the relative path.

fstream supports relative paths as shown below.

, , exe .

E:\MyProgramBin\YourExe.exe
E:\YourInputFile.ppm

, .

filename1 = "..\YourInputFile.ppm"

filename1 ifstream

std::ifstream src(filename1, std::ios_base::binary);
+1

Source: https://habr.com/ru/post/1526042/


All Articles