Data importing BUS ERROR

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • juleigha27@gmail.com

    #1

    Data importing BUS ERROR

    I can't figure out what is going on with this code. I don't know why
    it won't let me increment i without crashing. My data
    file looks like this:

    1343 5.66666 DOG
    2334 3.44444 FROG

    PLEASE help...I am out of ideas





    /* 1:45 1.17.05 */


    #include<stdio. h>
    #include<string .h>



    void main()
    {
    FILE *pfile1 = NULL;
    int i;

    /* Structure and File Declarations */
    struct salt_list
    {
    char name[5];
    int sample_mass;
    float sample_intensit y;

    };

    struct salt_list *psalt_list[1000];


    /*-----------------------------------------------------*/

    /* allocate memory to hold structure */

    psalt_list[i] = (struct salt_list*)mall oc(sizeof(struc t salt_list));

    pfile1 = fopen("data.txt ", "r");

    if (pfile1 == NULL)
    {
    printf("Error: can't open file.\n");

    }

    while(!feof(pfi le1)){






    fscanf(pfile1," %d%f%s",&psalt_ list[i]->sample_mass,&p salt_list[i]->sample_intensi ty,
    psalt_list[i]->name);
    i++; //here is the problem when I increase
    //the counter I get a bus error
    // otherwise I can run through the file
    // overwriting as i stays at i = 0;
    }

    printf("%s\n",p salt_list[i]->name);
    printf("%d\n",p salt_list[i]->sample_mass) ;
    printf("%f\n", psalt_list[i]->sample_intensi ty);

    }

  • Peter Nilsson

    #2
    Re: Data importing BUS ERROR

    juleigh...@gmai l.com wrote:[color=blue]
    > I can't figure out what is going on with this code. I don't know why
    > it won't let me increment i without crashing.
    >
    > int i;
    > ...
    > i++; //here is the problem when I increase[/color]

    Because you never initialise i. Automatic (non-static, non-extern
    block scope) variables will not be initialised by default. A good
    compiler will warn you against this.
    [color=blue]
    > while(!feof(pfi le1)){[/color]

    This is a basic error, mentioned in the clc FAQ.
    You should also check the return value of fscanf.

    --
    Peter

    Comment

    • Al Bowers

      #3
      Re: Data importing BUS ERROR



      juleigha27@gmai l.com wrote:[color=blue]
      > I can't figure out what is going on with this code. I don't know why
      > it won't let me increment i without crashing. My data
      > file looks like this:
      >
      > 1343 5.66666 DOG
      > 2334 3.44444 FROG
      >
      > PLEASE help...I am out of ideas
      >[/color]

      Well for one i is never initialized. It needs to be be zero at
      start.
      int i = 0;

      There are other errors listed below.
      [color=blue]
      >
      > /* 1:45 1.17.05 */
      >
      >
      > #include<stdio. h>
      > #include<string .h>[/color]

      You need to add
      #inclulde <stdlib.h>
      [color=blue]
      >
      >
      > void main()[/color]

      int main(void)[color=blue]
      > {
      > FILE *pfile1 = NULL;
      > int i;
      >
      > /* Structure and File Declarations */
      > struct salt_list
      > {
      > char name[5];
      > int sample_mass;
      > float sample_intensit y;
      >
      > };
      >
      > struct salt_list *psalt_list[1000];[/color]

      Instead of using an array of the fixed number of the struct pointers,
      you can dynamically allocated and array of the struct object,
      reallocating as it grows. See the example below.[color=blue]
      >
      > /*-----------------------------------------------------*/
      >
      > /* allocate memory to hold structure */
      >
      > psalt_list[i] = (struct salt_list*)mall oc(sizeof(struc t salt_list));
      > pfile1 = fopen("data.txt ", "r");
      >
      > if (pfile1 == NULL)
      > {
      > printf("Error: can't open file.\n");
      >
      > }
      >
      > while(!feof(pfi le1)){[/color]

      The loop is flawed. See faq question 12.2 at:


      [color=blue]
      >
      >
      >
      >
      >
      > fscanf(pfile1," %d%f%s",&psalt_ list[i]->sample_mass,&p salt_list[i]->sample_intensi ty,
      > psalt_list[i]->name);
      > i++; //here is the problem when I increase
      > //the counter I get a bus error
      > // otherwise I can run through the file
      > // overwriting as i stays at i = 0;
      > }[/color]

      You have incremented i to a position in the array that
      has not been allocated. The prints below will fail.
      [color=blue]
      > printf("%s\n",p salt_list[i]->name);
      > printf("%d\n",p salt_list[i]->sample_mass) ;
      > printf("%f\n", psalt_list[i]->sample_intensi ty);
      >[/color]

      Close the file and free the pointers.
      return 0;
      [color=blue]
      > }[/color]

      #include<stdio. h>
      #include<string .h>
      #include <stdlib.h>

      struct salt_list
      {
      char name[16];
      int sample_mass;
      float sample_intensit y;
      };

      int main(void)
      {
      FILE *pfile1;
      char buf[156];
      int i,j;
      struct salt_list *psalt_list, *tmp;

      pfile1 = fopen("data.txt ", "r");
      if (pfile1 == NULL)
      {
      printf("Error: can't open file.\n");
      exit(EXIT_FAILU RE);
      }
      for(i = 0,psalt_list = NULL; fgets(buf,sizeo f buf,pfile1);i++ )
      {
      tmp = realloc(psalt_l ist,(i+1)*sizeo f *psalt_list);
      if(tmp == NULL)
      {
      puts("Error: memory allocation failure");
      free(psalt_list );
      fclose(pfile1);
      exit(EXIT_FAILU RE);
      }
      psalt_list = tmp;
      if(3 != sscanf(buf,"%d% f%s",&psalt_lis t[i].sample_mass,
      &psalt_list[i].sample_intensi ty,
      psalt_list[i].name))
      {
      puts("Error: File format error");
      fclose(pfile1);
      free(psalt_list );
      exit(EXIT_FAILU RE);
      }
      }
      fclose(pfile1);
      for(j = 0; j < i; j++)
      printf("Name: %s \tMass: %d \tIntensity: %f\n",
      psalt_list[j].name,psalt_lis t[j].sample_mass,
      psalt_list[j].sample_intensi ty);
      free(psalt_list );
      return 0;
      }


      --
      Al Bowers
      Tampa, Fl USA
      mailto: xabowers@myrapi dsys.com (remove the x to send email)
      Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


      Comment

      • Old Wolf

        #4
        Re: Data importing BUS ERROR

        juleigh...@gmai l.com wrote:[color=blue]
        > I can't figure out what is going on with this code.
        > I don't know why it won't let me increment i without crashing.[/color]

        Your main problem is that you didn't allocate memory properly.
        You declare an array of 1000 pointers to struct.
        Then you malloc memory for 1 struct, leaving 999
        pointers still uninitialized.
        So when you go onto the second struct (by doing i++), you
        start working on random memory, causing a bus fault.

        I wonder why you are declaring 1000 pointers. It would be a
        far simpler design to have an array of 1000 structs and not
        use malloc at all.
        Alternatively you could have a pointer to an array of 1000
        structs, and allocate that array all in one go with malloc.

        You have three other major problems: you call malloc() wrongly,
        and you use feof() wrongly, and you don't check for overflows
        in fscanf() . You would learn the most by reading this
        newsgroup's FAQ, especially the sections on "malloc" and
        "file i/o".

        Comment

        Working...