Hi all,
Need some help in c. I have not been in programming for ten years. Here is my problem:
From main(), I call a function - a_function(). In a_function(), I call another function b_function() and then I allocate memory for a structure using calloc() in b_function(). Data is then copied to the structure. Structure is then return to the calling function which is a_function(). I then open a file for writing and then use fprintf() to write to the opened file. When I did all this, I get segmentation fault at the fprintf() statement. A very simple write statement -
a_function()
{
structure = b_function();
FILE *fout;
if((fout = fopen("test.dat", "w")) == NULL)
printf("can't open file.\n");
fprintf(fout, "testing\n");
fclose(fout);
}
structure_pointer *b_function()
{
declare structure pointer;
allocate memory for the structure;
copy the data;
return structure pointer;
}
I did check the file pointer and it is ok. memory allocation is ok. copy the data is ok. structure point return is ok.
If I open the file before b_function() and write to the file, there is no problem.
Thanks all ....