473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

can't close a file??

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

Nov 13 '05 #1
16 6872

"Michelle" <mi************@hotmail.com> wrote in message
news:3F**************@hotmail.com...
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
Nov 13 '05 #2


Michelle wrote:

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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 13 '05 #3

"Michelle" <mi************@hotmail.com> wrote in message
news:3F**************@hotmail.com...
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
Nov 13 '05 #4


Michelle wrote:

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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 13 '05 #5
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)[i]= 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

Fred L. Kleinschmidt wrote:

Michelle wrote:
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.

Nov 13 '05 #6
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)[i]= 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

Fred L. Kleinschmidt wrote:

Michelle wrote:
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.

Nov 13 '05 #7
Michelle <mi************@hotmail.com> wrote in
news:3F************@hotmail.com:
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)[i]= 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.

--
- Mark ->
--
Nov 13 '05 #8
Michelle <mi************@hotmail.com> wrote:

<code snipped>

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
--
Great minds run in great circles.
Nov 13 '05 #9
Michelle <mi************@hotmail.com> wrote in
news:3F************@hotmail.com:
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)[i]= 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.

--
- Mark ->
--
Nov 13 '05 #10
Michelle <mi************@hotmail.com> wrote:

<code snipped>

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
--
Great minds run in great circles.
Nov 13 '05 #11
"Mark A. Odell" <no****@embeddedfw.com> wrote:
Michelle <mi************@hotmail.com> wrote:
void getNewDeploy(DeployType *deploy ) {
<SNIP>
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.


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

<SNIP>
}


Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #12
"Mark A. Odell" <no****@embeddedfw.com> wrote:
Michelle <mi************@hotmail.com> wrote:
void getNewDeploy(DeployType *deploy ) {
<SNIP>
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.


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

<SNIP>
}


Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #13
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in
news:7d********************************@4ax.com:
void getNewDeploy(DeployType *deploy ) {
<SNIP>
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.


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?
--
- Mark ->
--
Nov 13 '05 #14
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in
news:7d********************************@4ax.com:
void getNewDeploy(DeployType *deploy ) {
<SNIP>
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.


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?
--
- Mark ->
--
Nov 13 '05 #15
"Mark A. Odell" <no****@embeddedfw.com> wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in
news:7d********************************@4ax.com :
void getNewDeploy(DeployType *deploy ) {

<SNIP>

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.


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?


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

Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #16
"Mark A. Odell" <no****@embeddedfw.com> wrote:
Irrwahn Grausewitz <ir*****************@freenet.de> wrote in
news:7d********************************@4ax.com :
void getNewDeploy(DeployType *deploy ) {

<SNIP>

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.


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?


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

Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Harry Pehkonen | last post by:
Stats: Python2.3 windows2000 professional If I have ``Full Control'' of a bsddb file, no problem: >>> import bsddb >>> a = bsddb.btopen("c:/sharedrw/db/npanxx2pseudo.db", "r") >>> a.close()...
2
by: Irvin Amoraal | last post by:
Process: I have a form which uploads a file from client to server written in PHP. When the user presses the submit button, I use the "onSubmit" event to execute javascript to open a child window...
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
10
by: Venkat | last post by:
Hi, I am using ifstream to open a file read it and then close it. Sample Code ------------------- ifStream inFile("file1.txt"); ....... .......
9
by: charliewest | last post by:
Hello - I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#) is there any way to temporarily save an image to a session object, and after running some other operations, later...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
3
by: emman_54 | last post by:
Hi every one, I am trying to run a batch file using my asp.net application. I am using the Process class to run the batch file. When I run my web application, In the task manager, i could see...
4
by: mammen | last post by:
Hello, I'm writing trace log of various actions happening in my ASP.NET web application by opening a text file when the user logs in to the system and closing the file while the user logs out....
2
by: necron99 | last post by:
import RuntimeEnvironment as renv import os, sys import win32com.client import getpass OMI = win32com.client.Dispatch("MOVEitAPI.clientObj") OMI.Host = system
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.