BUS ERROR

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neel

    BUS ERROR

    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(err no)<<endl;
    exit(0);
    }
    close(tmp->sock);
    }
    else{
    while(!feof(fp) ){
    fgets(myData,10 0,fp);
    strcat(infoToSe nd,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
  • Ian Collins

    #2
    Re: BUS ERROR

    Neel wrote:
    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".

    <ignoring socket specific stuff>
    else{
    while(!feof(fp) ){
    fgets(myData,10 0,fp);
    strcat(infoToSe nd,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++....

    --
    Ian Collins.

    Comment

    • Keith Thompson

      #3
      Re: BUS ERROR

      Neel <a.k.vora@gmail .comwrites:
      [...]
      while(!feof(fp) ){
      fgets(myData,10 0,fp);
      strcat(infoToSe nd,myData);
      }
      (I've changed the indentation.)

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

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Nick Keighley

        #4
        Re: BUS ERROR

        On 25 Sep, 05:58, Neel <a.k.v...@gmail .comwrote:
        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(infoToSe nd, 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

        Comment

        • bharat

          #5
          Re: BUS ERROR

          On Sep 25, 11:34 am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
          wrote:
          On 25 Sep, 05:58, Neel <a.k.v...@gmail .comwrote:
          >
          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(infoToSe nd, 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
          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

          Comment

          • Nick Keighley

            #6
            Re: BUS ERROR

            On 25 Sep, 11:30, bharat <bshe...@gmail. comwrote:
            On Sep 25, 11:34 am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
            wrote:
            On 25 Sep, 05:58, Neel <a.k.v...@gmail .comwrote:
            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(infoToSe nd, 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

            Comment

            • CBFalconer

              #7
              Re: BUS ERROR

              Nick Keighley wrote:
              Neel <a.k.v...@gmail .comwrote:
              >
              >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:
              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 ...
              >
              cout << "Error: " << strerror(errno) << endl;
              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.

              Comment

              • Nick Keighley

                #8
                Re: BUS ERROR

                On 25 Sep, 15:54, CBFalconer <cbfalco...@yah oo.comwrote:
                Nick Keighleywrote:
                Neel <a.k.v...@gmail .comwrote:
                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:
                >
                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 ...
                >
                            cout << "Error: " << strerror(errno) << endl;
                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.


                --
                Nick Keighley

                Comment

                • David Thompson

                  #9
                  Re: BUS ERROR

                  On Thu, 25 Sep 2008 03:30:46 -0700 (PDT), bharat <bshetty@gmail. com>
                  wrote:
                  On Sep 25, 11:34 am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
                  wrote:
                  On 25 Sep, 05:58, Neel <a.k.v...@gmail .comwrote:
                  <snip rather confused and incomplete code>
                  >
                  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.ne t

                  Comment

                  Working...