0

I am trying to read ppm files called bridge.ppm or retina.ppm and display them using a C program on a windows machine using Visual Studio 2019. Whenever I am trying to read the file using fopen I get an error. Here is the code for the same:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR     lpCmdLine, int nCmdShow)
{
FILE *fpt;
unsigned char   *image;
char        header[80];
int     ROWS,COLS,BYTES;
unsigned char *displaydata;
char fileName[128];
errno_t err;


if (MessageBox(NULL, L"Is this a greyscale image?", L"Greyscale", MB_YESNO) == IDYES)
strcpy(fileName,"bridge.ppm");
else
strcpy(fileName,"retina.ppm");
err= fopen_s(&fpt,fileName, "rb");
if (err)
{
    printf("error %d \n",err);
    printf("Unable to open %s for reading\n", fileName);
    exit(0);
}
}

Every time I get err 2, File Not Found Error. The project directory looks like this in the solution explorer: Project Structure as shown in solution explorer

To Debug this, I have done the following :

  • I tried to read a different file named "test.txt" and successfully read it.
  • To have files present in the same directory as the executable output. I placed both of these files in the Debug folder, but the result was the same.
  • I tried to provide the full path of the image files but got the same error.
  • I changed the fopen mode to 'r' but got the same error.
  • Change the permission of the file to Full access.

I am not that experienced with Visual Studio and may have missed some details. Please let me know.

titor
  • 35
  • 1
  • 8
  • What error are you getting? – Eugene Sh. Jan 31 '22 at 18:37
  • 4
    Most likely, the directory where the `.ppm` file is stored is not the current directory when the program is run. Consider specifying the full (absolute) pathname of the `.ppm` file. You say you've tried to colocate the files and the program, but every IDE I've used has a mind of its own. Try running the program from a terminal window (command prompt). – Jonathan Leffler Jan 31 '22 at 18:40
  • @EugeneSh. When printing the value of err, it is 2, According to this stack overflow answer this is [link](https://stackoverflow.com/a/15753101/6063389) probably FIle Not Found Error. – titor Jan 31 '22 at 18:57
  • @JonathanLeffler I tried using gcc and the code was working fine, but I have to run the code in a windows development environment and I was getting linking errors when using the cl windows compiler. – titor Jan 31 '22 at 18:58
  • 1
    As noted, the problem is most likely a 'current working directory' issue. Also, you may want to take a look at [this code](https://github.com/microsoft/DirectXTex/blob/main/Texconv/PortablePixMap.cpp) which implements PPM reading and writing. – Chuck Walbourn Feb 01 '22 at 00:32
  • Yeah the problem was the working directory, I had it in the Debug folder but you have to put them in the project root directory. – titor Feb 01 '22 at 04:15

0 Answers0