can't close a file??

M

Michelle

Hi,

When i compiled a program, i got a segmentation fault as follows:

Program received signal SIGSEGV, Segmentation fault.
0x42074bd0 in _int_free () from /lib/i686/libc.so.6

#0 0x42074bd0 in _int_free () from /lib/i686/libc.so.6
#1 0x42075a5c in free () from /lib/i686/libc.so.6
#2 0x4206293c in fclose@@GLIBC_2.1 () from /lib/i686/libc.so.6
#3 0x08049a76 in getNewDeploy (deploy=0x804b450) at redeploy.c:839
#4 0x08049867 in generate_table (dif=0xbfffde80) at redeploy.c:725
#5 0x08048786 in main () at redeploy.c:63
#6 0x420158f7 in __libc_start_main () from /lib/i686/libc.so.6


The error was caused by function fclose. In the program, i open a file,
read some data from it, then close it. I just checked using gdb. The
data can be read correctly. however when it comes to fclose, a seg fault
appears. Could sb. give me some ideas what may cause fclose fail? Thanks
in advance.

Best.
Michelle
 
M

Mike Wahler

Michelle said:
Hi,

When i compiled a program, i got a segmentation fault as follows:

Program received signal SIGSEGV, Segmentation fault.
0x42074bd0 in _int_free () from /lib/i686/libc.so.6

#0 0x42074bd0 in _int_free () from /lib/i686/libc.so.6
#1 0x42075a5c in free () from /lib/i686/libc.so.6
#2 0x4206293c in fclose@@GLIBC_2.1 () from /lib/i686/libc.so.6
#3 0x08049a76 in getNewDeploy (deploy=0x804b450) at redeploy.c:839
#4 0x08049867 in generate_table (dif=0xbfffde80) at redeploy.c:725
#5 0x08048786 in main () at redeploy.c:63
#6 0x420158f7 in __libc_start_main () from /lib/i686/libc.so.6


The error was caused by function fclose.

Highly unlikely. More likely, there's something wrong
with your code.

In the program, i open a file,

Did you check the return value from 'fopen()'?
read some data from it,

Did you check your read operations for success/failure?
then close it. I just checked using gdb. The
data can be read correctly. however when it comes to fclose, a seg fault
appears. Could sb. give me some ideas what may cause fclose fail? Thanks
in advance.

You have a bug on line 47. (I.e. how the *** can we know without
seeing any code?).

-Mike
 
F

Fred L. Kleinschmidt

Michelle said:
Hi,

When i compiled a program, i got a segmentation fault as follows:

Program received signal SIGSEGV, Segmentation fault.
0x42074bd0 in _int_free () from /lib/i686/libc.so.6

#0 0x42074bd0 in _int_free () from /lib/i686/libc.so.6
#1 0x42075a5c in free () from /lib/i686/libc.so.6
#2 0x4206293c in fclose@@GLIBC_2.1 () from /lib/i686/libc.so.6
#3 0x08049a76 in getNewDeploy (deploy=0x804b450) at redeploy.c:839
#4 0x08049867 in generate_table (dif=0xbfffde80) at redeploy.c:725
#5 0x08048786 in main () at redeploy.c:63
#6 0x420158f7 in __libc_start_main () from /lib/i686/libc.so.6

The error was caused by function fclose. In the program, i open a file,
read some data from it, then close it. I just checked using gdb. The
data can be read correctly. however when it comes to fclose, a seg fault
appears. Could sb. give me some ideas what may cause fclose fail? Thanks
in advance.

Best.
Michelle

It would help if you showed us your code.
 
M

Michelle

Hi, i attach my code here:

////////////////////////

void getNewDeploy(DeployType *deploy ) {

DeployRecType de;
AInTablePtr newptr;

int MAX_LINE_LENGTH=300;

char line[MAX_LINE_LENGTH];

char delim[] = ":";
char *tempagent, *tempstring;
char *tmpstr;

FILE *fpd;


if((fpd = fopen(DEPLOY_FILE, "r")) == NULL){
printf("\n file couldn't be found.");
exit(1);
}


int i;
for(i=0;i<MAXNODES;i++){

sprintf(de.node, "%d", i+1);
tmpstr = de.node;
newptr = NULL;

while (fgets(line, MAX_LINE_LENGTH, fpd)!= NULL){

if( strstr(line, delim) !=NULL ){
tempagent=strtok(line, delim);
tempstring=strtok(NULL,delim);

if( strstr(tempstring, tmpstr) != NULL ){
addtotable(&newptr, tempagent); ;
}
}
}

de.agents = newptr;

(*deploy)= de;

fseek(fpd,0,SEEK_SET);
}


if( fclose(fpd) != 0 )

printf( "Can't close file!" );

}

////////////////////////////////////////////////////////////


All are fine until it goes to if(fclose(fpd)!=0), where a seg fault
appears and it even doesn't print out "Can't close file!"

I guess there must be something wrong on pointers. however, i really
have no idea where the bug is...

Thanks!

michelle
 
M

Mark A. Odell

Hi, i attach my code here:

////////////////////////

void getNewDeploy(DeployType *deploy ) {

DeployRecType de;
AInTablePtr newptr;

int MAX_LINE_LENGTH=300;

char line[MAX_LINE_LENGTH];

char delim[] = ":";
char *tempagent, *tempstring;
char *tmpstr;

FILE *fpd;


if((fpd = fopen(DEPLOY_FILE, "r")) == NULL){
printf("\n file couldn't be found.");
exit(1);
}


int i;

Hey! You can't do this in C. You have defined a new variable without an
enclosing block -- syntax error unless this is really C++ in disguise.
for(i=0;i<MAXNODES;i++){

sprintf(de.node, "%d", i+1);
tmpstr = de.node;
newptr = NULL;

while (fgets(line, MAX_LINE_LENGTH, fpd)!= NULL){

if( strstr(line, delim) !=NULL ){
tempagent=strtok(line, delim);
tempstring=strtok(NULL,delim);

if( strstr(tempstring, tmpstr) != NULL ){
addtotable(&newptr, tempagent); ;
}
}
}

de.agents = newptr;

(*deploy)= de;

fseek(fpd,0,SEEK_SET);
}


if( fclose(fpd) != 0 )

printf( "Can't close file!" );

}

////////////////////////////////////////////////////////////


All are fine until it goes to if(fclose(fpd)!=0), where a seg fault
appears and it even doesn't print out "Can't close file!"


Well I can't see it. Can you load up DDD, Insight, or gdb and see what
happens to the value of 'fpd' thoughout the program? I can only guess that
the fpd pointer is getting corrupted and you are attempting to close a
non-file or some other file you aren't allowed to.
 
I

Irrwahn Grausewitz

I guess there must be something wrong on pointers. however, i really
have no idea where the bug is...
Michelle,

what looks DeployRecTyp like?
What looks AInTablePtr like?
What does addtotable() do?
Returns it a value? If so, why didn't you check it?
Why do you rewind the file every time the for loop executes?

The problem is that you did not post a compilable code sample. And if
you want to post one, cut it down to a bare minimum - most likely you
will find the error yourself when doing so (and when you are at it,
apply some consistent indentation on your code :) ).

This is my tip for you:
- Start commenting out chunks of code.
- Compile and test
- When the error disappears, start putting in chunks of code again.

This way you should be able to locate the error.
Using a debugger may also be helpful... ;)

Regards

Irrwahn
 
I

Irrwahn Grausewitz

Mark A. Odell said:
Hey! You can't do this in C. You have defined a new variable without an
enclosing block -- syntax error unless this is really C++ in disguise.

No problem, as it is enclosed in a block. It's just the bad indentation.
:)


Irrwahn
 
M

Mark A. Odell

No problem, as it is enclosed in a block. It's just the bad indentation.

No, it isn't, in C you have to have a '{' before any variable definitions
without *any* run-time code between the '{' and the definition. E.g.

int main(void)
{
int idx; /* ok */

idx = 12;

char *pFoo; /* syntax error */

{
double fault; /* ok */

fault = 3.141592;

long boat; /* syntax error */
}

return 0;
}


Get the picture?
 
I

Irrwahn Grausewitz

Mark A. Odell said:
No, it isn't, in C you have to have a '{' before any variable definitions
without *any* run-time code between the '{' and the definition. E.g.

int main(void)
{
int idx; /* ok */

idx = 12;

char *pFoo; /* syntax error */

{
double fault; /* ok */

fault = 3.141592;

long boat; /* syntax error */
}

return 0;
}

Get the picture?

True for C89, but AFAIK the above example is perfectly valid in C99!

Regards

Irrwahn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top