BUS ERROR

N

Neel

Hi friends,
I 'm facing some weird problem with file handling.
When I open the file and read the contents, it works fine but when I
do it again for the second time In a loop, it fails...

my code is...
while(1){

fp=fopen("tram_schedule.txt","r");
if(fp==0){
char errMsg[]="Error: Unable to get tram schedules. pls try again
later";
puts("Error");
if(send(tmp->sock, errMsg, strlen(errMsg), 0)==-1){
cout<<"Error: "<<strerror(errno)<<endl;
exit(0);
}
close(tmp->sock);
}
else{
while(!feof(fp)){
fgets(myData,100,fp);
strcat(infoToSend,myData);
}
}
//puts(infoToSend);
cout<<"socket id: "<<tmp->sock<<endl;
fclose(fp);
}

'm opening and closing the file in every iteration.
can anyone please help me solve this issue?

Thanks in advance
 
I

Ian Collins

Neel said:
Hi friends,
I 'm facing some weird problem with file handling.
When I open the file and read the contents, it works fine but when I
do it again for the second time In a loop, it fails...

my code is...

Please don't post code with tabs.
while(1){

fp=fopen("tram_schedule.txt","r");
if(fp==0){
char errMsg[]="Error: Unable to get tram schedules. pls try again

Bug 1, you can't spell "please".

else{
while(!feof(fp)){
fgets(myData,100,fp);
strcat(infoToSend,myData);
}
}

What is myData and nfoToSend?

What ever nfoToSend is, it will keep growing each time round the loop.
//puts(infoToSend);
cout<<"socket id: "<<tmp->sock<<endl;

Tut tut, C++....
 
K

Keith Thompson

Neel said:
while(!feof(fp)){
fgets(myData,100,fp);
strcat(infoToSend,myData);
}

(I've changed the indentation.)

You don't want to use feof() to control an input loop. Check the
value returned by fgets().
 
N

Nick Keighley

Hi friends,
I 'm facing some weird problem with file handling.
When I open the file and read the contents, it works fine but when I
do it again for the second time In a loop, it fails...

my code is...

cleaned up code:

while (1)
{
fp = fopen ("tram_schedule.txt", "r");

if (fp == 0)
{
char errMsg[]=
"Error: Unable to get tram schedules. pls try again later";

puts("Error");

if (send (tmp->sock, errMsg, strlen(errMsg), 0 ) == -1)
{
cout << "Error: " << strerror(errno) << endl;
exit(0);
}

close (tmp->sock);
/*** you closed the socket. What happens if
there's an error next time around? ***/

}
else
{
while(!feof(fp))
{
fgets (myData, 100, fp);
strcat(infoToSend, myData);
}
}

//puts(infoToSend);
cout << "socket id: " << tmp->sock << endl;
fclose (fp);
/*** what if the file faile dto open? You fclose()ed a NULL
pointer ***/
}
 
B

bharat

Hi friends,
I 'm facing some weird problem with file handling.
When I open the file and read the contents, it works fine but when I
do it again for the second time In a loop, it fails...
my code is...

cleaned up code:

while (1)
{
    fp = fopen ("tram_schedule.txt", "r");

    if (fp == 0)
    {
        char errMsg[]=
        "Error: Unable to get tram schedules. pls try again later";

        puts("Error");

        if (send (tmp->sock, errMsg, strlen(errMsg), 0 ) == -1)
        {
            cout << "Error: " << strerror(errno) << endl;
            exit(0);
        }

        close (tmp->sock);
/*** you closed the socket. What happens if
there's an error next time around? ***/

    }
    else
    {
        while(!feof(fp))
        {
            fgets (myData, 100, fp);
            strcat(infoToSend, myData);
        }
    }

    //puts(infoToSend);
    cout << "socket id: " << tmp->sock << endl;
    fclose (fp);
/*** what if the file faile dto open? You fclose()ed a NULL
pointer ***/

}
'm opening and closing the file in every iteration.
can anyone please help me solve this issue?

I assume you mean to say that you are getting 'bus error'. The most
common way of getting this is when data access is misaligned. In
simple english it could mean that you are trying to access a wider
data with an address that aligns to a smaller width.

For eg., you are trying to access a four byte integer with say a char
pointer. In this case the char pointer is aligned on a single byte
border however the integer is to aligned against four bytes address.
This is a problem only with RISC boxes. Perhaps your university has a
RISC box.

And the snippet that you have provided is not very conducive to
debugging. you need to give the declarations at a minimum.

cheers,
Bharat
 
N

Nick Keighley

On 25 Sep, 05:58, Neel <[email protected]> wrote:
cleaned up code:
while (1)
{
fp = fopen ("tram_schedule.txt", "r");
if (fp == 0)
{
char errMsg[]=
"Error: Unable to get tram schedules. pls try again later";
puts("Error");

if (send (tmp->sock, errMsg, strlen(errMsg), 0 ) == -1)
{
cout << "Error: " << strerror(errno) << endl;
exit(0);
}
close (tmp->sock);
/*** you closed the socket. What happens if
there's an error next time around? ***/
}
else
{
while(!feof(fp))
{
fgets (myData, 100, fp);
strcat(infoToSend, myData);
}
}
//puts(infoToSend);
cout << "socket id: " << tmp->sock << endl;
fclose (fp);
/*** what if the file faile dto open? You fclose()ed a NULL
pointer ***/

don't quote sigs (the bit after "-- ")

I assume you mean to say that you are getting 'bus error'.

well *I* didn't intend to say I was getting a bus error
so I assume you were addressing the OP (Original Poster).

The most
common way of getting this is when data access is misaligned. In
simple english it could mean that you are trying to access a wider
data with an address that aligns to a smaller width.

For eg., you are trying to access a four byte integer with say a char
pointer. In this case the char pointer is aligned on a single byte
border however the integer is to aligned against four bytes address.

I think you have this backward

int i = 1;
char *p;
p = (char*)&i;
printf ("%c\n", *p);

"works" it may not access the byte you expected but it won't
bus error.

/* assuming int is 4 bytes */
char ia [4] = {1, 2, 3, 4};
int *pi = (int*)&ia[0];
printf ("%d\n", *pi);

may bus error is ia is not correctly aligned for an int

This is a problem only with RISC boxes.
hardly

Perhaps your university has a RISC box.

And the snippet that you have provided is not very conducive to
debugging. you need to give the declarations at a minimum.

100% agreement
 
C

CBFalconer

Nick said:
cleaned up code:

Not well enough. This is still C++. That benighted language is
normally discussed in comp.lang.c++. We try to keep this newsgroup
relatively clean. :=)
 
N

Nick Keighley

Nick Keighleywrote:


Not well enough.  This is still C++.  That benighted language is
normally discussed in comp.lang.c++.  We try to keep this newsgroup
relatively clean. :=)

... snip ...

yes I know. And other people had already pointed this out.
I don't see any point in repeating things. The points I made
are applicable to C.
 
D

David Thompson

I assume you mean to say that you are getting 'bus error'. The most
common way of getting this is when data access is misaligned. In
simple english it could mean that you are trying to access a wider
data with an address that aligns to a smaller width.
That is _a_ way, and historically the first, but nowadays, on machines
that typically have virtual address spaces quite a bit larger than
most programs need or use, hence sparsely mapped, that same signal or
equivalent and description is often used for the also-common case of
accessing an address that isn't mapped at all, usually either because
the pointer is complete garbage e.g. uninitialized auto or clobbered,
or it was stepped past (often pretty far past) the end of valid data.
It can also occur for a stale pointer, especially one into a heap (or
subheap) that's large enough it actually gets cut back.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top