Re: BUS ERROR
- From: Nick Keighley <nick_keighley_nospam@xxxxxxxxxxx>
- Date: Thu, 25 Sep 2008 06:58:03 -0700 (PDT)
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
.
- References:
- BUS ERROR
- From: Neel
- Re: BUS ERROR
- From: Nick Keighley
- Re: BUS ERROR
- From: bharat
- BUS ERROR
- Prev by Date: Re: good c compiler
- Next by Date: Re: Any way to take a word as input from stdin ?
- Previous by thread: Re: BUS ERROR
- Next by thread: Re: BUS ERROR
- Index(es):
Relevant Pages
|
Loading