Alain said:
I'm sorry.
Here's my further information:
The code compiles fine. The problem is that the program simply closes when
it should do the fread() part.
Here's the code fragment:
void abhaengen(FILE *fp_q, int key)
{
int i;
const char* file_qn;
Why const? This declaration says file_qn is a pointer to const char, which
means you cannot change what file_qn points to. Since this is precisely
what you want to do, lose the const:
char *file_qn;
However, you still haven't made file_qn point to anything useful. One way
of doing this is by using malloc to allocate some memory and make file_qn
point to that.
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
Now you even know how much memory you need (check the return value of fscanf
to make sure you got a valid integer):
file_qn = malloc(sizeof *file_qn * x); /* check the result for NULL */
fread(&file_qn, x, 1, fp_q); //this is exactly where the program
That's totally wrong. fread will read nmemb items each size bytes big into
the area pointed to by ptr:
fread(file_qn, 1, x, fp_q);
Which says, read x items of size 1 from fp_q into the area pointed to by
file_qn. However, note that the fscanf will still leave any spaces between
the integer and the filename in the input stream, which means that simple
fread will get leading blank(s) and part of the filename. You're probably
better off doing this:
fscanf(fp_q, "%*[\n]"); /* throw away any blanks */
fread(&file_qn, x, 1, fp_q); /* now read the filename */
simply
closes with no further messages.
Probably a result of accessing memory you don't own through that fread call.
One would hope a decent OS will give you some indication as to why the
program was killed.
HTH,
-nrk.