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);
}
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);
}
Comment