error fopen with existing file

This is a discussion on error fopen with existing file within the C Programming forums, part of the General Programming Boards category; Hello,
I am having a problem when I try to open a file from within a calling function.
I have ...

error fopen with existing file

Hello,

I am having a problem when I try to open a file from within a calling function.

I have a main function that defines part of the string name of a file (the file already exists in the working directory), passes this name to the function openmyfile(), and openmyfile uses strcat to give the correct extension to the name. When though openmyfile calls fopen, the file cannot be read.

I put a sample of what main.c does and how openmyfile works.

It is the first time that I happen to not be able to open an existing file for reading.
Thank you in advance

First off, avoid casting the return value of malloc. Second, sizeof(char*) is likely 4, I believe you were trying to say sizeof(char) instead but even this would not be needed since it's guaranteed (I think) to be 1. I'm guessing those statements should simply be:

Code:

problem_name = malloc(16);
...
complete_filename = malloc(32);

#2

Code:

strcat(complete_filename, problem_name);

strcat depends on the array complete_filename being either empty or containing a properly terminated string. In either case, you'd expect a null to be in that array someplace but since you've just malloc'd and not initialized it you can't be sure what's in it at all. A better choice here would likely be strcpy instead but it doesn't really seem to be needed at all (just use problem_name directly in the fopen call). Were you attempting to concatenate something else to the string passed into the function?

#3 You're missing a call to free in the openmyfile function for complete_filename.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

First off, avoid casting the return value of malloc. Second, sizeof(char*) is likely 4, I believe you were trying to say sizeof(char) instead but even this would not be needed since it's guaranteed (I think) to be 1. I'm guessing those statements should simply be:

Code:

problem_name = malloc(16);
...
complete_filename = malloc(32);

#2

Code:

strcat(complete_filename, problem_name);

strcat depends on the array complete_filename being either empty or containing a properly terminated string. In either case, you'd expect a null to be in that array someplace but since you've just malloc'd and not initialized it you can't be sure what's in it at all. A better choice here would likely be strcpy instead but it doesn't really seem to be needed at all (just use problem_name directly in the fopen call). Were you attempting to concatenate something else to the string passed into the function?

I actually need to concatenate because the string name at hand depends on something that is done beforehand inside this function. About that I cant really do otherwise.

#3 You're missing a call to free in the openmyfile function for complete_filename.

Sorry about the lack in this forum; in the original complete code I am actually freeing correctly.
Thanks for the suggestion though.

I tried to print to screen what the variable "complete_filename" is assigned before passing it to "fopen", and I obtain that the name Io exopect to receive is correct, although I am afraid there must be some EOF or end of string somewhere in it.

I report here the output of my printing the variable string, and the error of fopen:

I tried to print to screen what the variable "complete_filename" is assigned before passing it to "fopen", and I obtain that the name Io exopect to receive is correct, although I am afraid there must be some EOF or end of string somewhere in it.

I report here the output of my printing the variable string, and the error of fopen: