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