Problem with variable and fopen VC ++

2

I've been breaking my head with this problem because if I occupy Codeblocks with wingw it does not throw me any problems

char *getArchivoSalida(int num)
{
    char nombre[1024] = "";
    sprintf(nombre, "Imagenx%d.ppm", num);
    ofstream fs(nombre);
    fs.close();
    return nombre;
}

int binario(char *nombre, int n)
{
    FILE *entrada, *salida;
    unsigned char red, green, blue;
    char *archivoSalida = "";
    char formato[3];
    int height, width, depth;
    int widthCont = 1;
    entrada = fopen(nombre, "rb");
    if (entrada == NULL)
    {
        printf("NO SE PUDO ABRIR\n");
        system("pause");
        return -1;
    }
    archivoSalida = getArchivoSalida(n);
    salida = fopen(archivoSalida ,"w+");
    fscanf(entrada, "%s", formato);
    /Continua abajo/
}

The problem is that in the line of salida = fopen(archivoSalida, "w+") when you see the error in the compiler it appears that the vaiable% FILE output contains a value null thing that should receive the name of the file. I do not know how it gets lost, I tried to put it as a constant variable but nothing

The last straw is that if I enter

salida = fopen("Imagenx1.ppm", "w+");

works perfectly ....

what's going on, is it a vc ++ problem?

greetings

Edition:

    
asked by 44teru 12.01.2017 в 18:55
source

1 answer

0

In the comments you have the clue: you are returning an internal variable from the getArchivoSalida( ) function.

That means that, when calling that function, you get a invalid string (more correctly, of undefined content ).

When trying the fopen( ) , NO you are passing the nombre , which is what you expect, but something that you can not control. As a result, fopen( ) fails, and returns NULL .

Keep in mind that no compiler is wrong. undefined content means that both behaviors are correct. The problem is not the compiler.

In fact, I'm sure that both compilers will show you a warning when compiling, of type intento de retornar una variable interna de la función or similar, or your equivalent in English.

In this question Problem when sending pointer to array as a parameter to a function you have a more detailed description of your problem, and some possible solutions.

    
answered by 12.01.2017 / 20:33
source