Please help me out of the problem.

Hi, I practiced programming c++ with visual studio code on Mac. But when I tried to debug the code as below, it always come out with "failed to open file!". It seems quite strange. Pl help me. thx a lot

1
2
3
4
5
6
7
8
  const char* filename ="file://Users/*****/***.txt";
    FILE* fp= fopen(filename, "wb");
    int a=1;
    if (fp == NULL)
    {
       printf("failed to open file!\n");
       return -1;
    }
"file://Users/*****/***.txt" seems odd. I would expect something like "/Users/*****/***.txt"
fopen() will set errno to tell you exactly what went wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <cerrno>
#include <cstdio>
#include <cstring>

int main()
{
    const char* filename ="file://Users/*****/***.txt";
    FILE* fp= fopen(filename, "wb");
    int a=1;
    if (fp == NULL)
    {
	printf("failed to open file: %s:", strerror(errno));
       return -1;
    }
}

Last edited on
Topic archived. No new replies allowed.