Re: BUS ERROR



On 25 Sep, 11:30, bharat <bshe...@xxxxxxxxx> wrote:
On Sep 25, 11:34 am, Nick Keighley <nick_keighley_nos...@xxxxxxxxxxx>
wrote:
On 25 Sep, 05:58, Neel <a.k.v...@xxxxxxxxx> wrote:


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?

--
Nick Keighley

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


--
Nick Keighley

.



Relevant Pages

  • problem with free.
    ... I am getting bus error, if I include the line "free", in the ... int main ... I have another function "func" which returns the result as a char*. ...
    (comp.lang.c)
  • Re: Accessing void * buffer/array through char * pointer
    ... byte 1: (null char) ... But if I'd change the initializer to something like ... Is some kind of overflow happening when I subscript the char pointer? ... have array of chars or int the linker allocates bytes of initialized ...
    (comp.lang.c)
  • Re: Accessing void * buffer/array through char * pointer
    ... int main ... printf(" (null char)"); ... byte 1: (null char) ... Is some kind of overflow happening when I subscript the char pointer? ...
    (comp.lang.c)
  • Accessing void * buffer/array through char * pointer
    ... int main ... printf(" (null char)"); ... byte 1: (null char) ... Is some kind of overflow happening when I subscript the char pointer? ...
    (comp.lang.c)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)

Loading