Data importing BUS ERROR

J

juleigha27

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_intensity;

};

struct salt_list *psalt_list[1000];


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

/* allocate memory to hold structure */

psalt_list = (struct salt_list*)malloc(sizeof(struct salt_list));

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

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

}

while(!feof(pfile1)){






fscanf(pfile1,"%d%f%s",&psalt_list->sample_mass,&psalt_list->sample_intensity,
psalt_list->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",psalt_list->name);
printf("%d\n",psalt_list->sample_mass);
printf("%f\n", psalt_list->sample_intensity);

}
 
P

Peter Nilsson

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

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.
while(!feof(pfile1)){

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

Al Bowers

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

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

There are other errors listed below.
/* 1:45 1.17.05 */


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

You need to add
#inclulde said:
void main()

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

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

};

struct salt_list *psalt_list[1000];

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.
/*-----------------------------------------------------*/

/* allocate memory to hold structure */

psalt_list = (struct salt_list*)malloc(sizeof(struct salt_list));
pfile1 = fopen("data.txt", "r");

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

}

while(!feof(pfile1)){


The loop is flawed. See faq question 12.2 at:
http://www.eskimo.com/~scs/C-faq/q12.2.html

fscanf(pfile1,"%d%f%s",&psalt_list->sample_mass,&psalt_list->sample_intensity,
psalt_list->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;
}


You have incremented i to a position in the array that
has not been allocated. The prints below will fail.
printf("%s\n",psalt_list->name);
printf("%d\n",psalt_list->sample_mass);
printf("%f\n", psalt_list->sample_intensity);


Close the file and free the pointers.
return 0;

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

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

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_FAILURE);
}
for(i = 0,psalt_list = NULL; fgets(buf,sizeof buf,pfile1);i++)
{
tmp = realloc(psalt_list,(i+1)*sizeof *psalt_list);
if(tmp == NULL)
{
puts("Error: memory allocation failure");
free(psalt_list);
fclose(pfile1);
exit(EXIT_FAILURE);
}
psalt_list = tmp;
if(3 != sscanf(buf,"%d%f%s",&psalt_list.sample_mass,
&psalt_list.sample_intensity,
psalt_list.name))
{
puts("Error: File format error");
fclose(pfile1);
free(psalt_list);
exit(EXIT_FAILURE);
}
}
fclose(pfile1);
for(j = 0; j < i; j++)
printf("Name: %s \tMass: %d \tIntensity: %f\n",
psalt_list[j].name,psalt_list[j].sample_mass,
psalt_list[j].sample_intensity);
free(psalt_list);
return 0;
}
 
O

Old Wolf

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.

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".
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top