472,958 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Segmentation fault - fprintf

1
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 ....
Jun 8 '07 #1
1 4783
mac11
256 100+
Just a guess, but maybe something in b_function() is mangling your program memory in a way you haven't considered. Perhaps your memory copy step is faulty. I once had a piece of code that managed to mangle my program's call stack causing it to crash at seemingly benign places.

I'd run a debugger and look at everything stack, heap, local memory, etc starting from before you go into b_function()
Jun 8 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Adi | last post by:
hi guys, i have a weird problem with printf statement. I have a function which just prints a string literal. In my program this function will be called > 2000 times. I get a segmentation fault...
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
19
by: Sameer | last post by:
Hi friends, I am using Mandriva Linux 9.2 and gcc. My source code is, int chunkin ; //no error int i ; for (i=0;i<7225;i++) { chunkin = somedata ;
1
by: didier2309 | last post by:
I get segemntation fault when I enter that function it executes till wcpcpy(lacc_num, L"000-00-0001"); Here is the function: It executes till the statement wcpcpy(laccnum, L"000-00-0001"); int...
6
by: SP | last post by:
The following code compiles with no errors, but fails with "Segmentation Fault" when I run it . I tracked the problem down to the code which populates the image.pixel_p array and I assume that the...
3
by: wastedhello | last post by:
hi im farely new to programming, doing it for university, but i cant figure out why this isnt working. it comes up with segmentation faults. can anyone help #include <stdio.h> #include...
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.