I've searched through most of the fopen questions throughout the web with no avail. I am trying to open a file with fopen. Below is a portion of my code:
FILE *filep = NULL;
FILE *Compilation = NULL;
printf("%s\n", fname);
char *pfname = (char*)malloc(sizeof(path) + sizeof(fname));
pfname = concat(path, fname);
printf("%s\n", pfname);
filep = fopen(pfname,"r");
if (filep == NULL){
printf("opening file failed: %s\n", strerror(errno));
}
printf("%p\n", filep);
I'm using Visual Studio express 2012 as my IDE. When I run this without debug. It crashes after printing the concatenated pfname. However, when I run this in debug mode, it works. filep does not return NULL and it will printf the memory address. I have another fopen after the printf and in debug mode it also works. Can anyone help me understand what is going on? The file is there, the permissions are right. This is newer revision of the code but previously before some untracked changes, the fopen worked.
Thanks in advance.
revised code
FILE *filep = NULL;
FILE *Compilation = NULL;
printf("%s\n", fname);
char *pfname = (char*)malloc(strlen(path) + strlen(fname) + 1);
pfname = concat(path, fname);
printf("%s\n", pfname);
filep = fopen(pfname,"r");
if (filep == NULL){
printf("opening file failed: %s\n", strerror(errno));
}
printf("%p\n", filep);
char *newfile = (char*)malloc(strlen(path) + 11);
newfile = concat(path, "Result.txt");
Compilation = fopen(newfile, "a");
It still can't get get past fopen because it does not print the pointer.
Answer
The problem is this line:
char *pfname = (char*)malloc(sizeof(path) + sizeof(fname));
The sizeof
function in C is the size of the data type. In this case, the data type is a pointer (I'm assuming). So it is 4 on a 32-bit system, or 8 on a 64-bit system. I've seen this misconception a few times here on StackOverflow.com for what sizeof
means.
To correct it, you need:
char *pfname = (char*)malloc(strlen(path) + strlen(fname) + 1);
Because your final string (that pfname
points to) must be the length of both strings, plus the byte to hold the zero termination.
Your existing line:
pfname = concat( path, file );
overwrites the allocated pointer that was put into pfname
. The new file path should be computed as follows:
strcpy( pfname, path ); // Copy the path to the newly allocated file path buffer
strcat( pfname, file ); // Concatenate the file name to the path
I'm assuming path
ends in the required directory delimeter character. Otherwise that will need to be calculated in by allocating an additional char for pfname
and concatenating a \
.
The same logic will apply to your subsequent file logic for Compilation.
No comments:
Post a Comment