Yasir Mehmood Yasir Mehmood - 1 month ago 12
Linux Question

smashing stack detected program terminated in c

I am getting this error *****smashing stack detected******program terminated .I am not using any buffer.Just reading record by record.Please help me.
Here is my code:

#include <stdio.h>

struct art
{
char name[30];
float velocity;
float min_angle;
float max_angle;
};
create(char *,float,float,float); */
int main()
{

// printf("I am starting");

FILE *infile;
struct art input;

/*** open the accounts file ***/
infile = fopen ("artillery.txt","r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening artillery.txt\n\n");
// exit (1);
}

while (fread (&input, sizeof(struct art), 6, infile))
{
printf("%s", input.name);
printf("%f", input.velocity);
printf("%f", input.min_angle);
printf("%f", input.max_angle);
}
return 0;
}


file records are:

canon 1700 30 45
bazooka 80 30 45
tank 2000 33 50
ak-47 715 0 90
pistol9mm 380 0 90
revolver 400 0 90

Answer

With this

fread (&input, sizeof(struct art), 6, infile)

you are trying to read 6 records into a variable which is a single struct. To read 6 records, you would need an array of 6 structs

struct art input[6];

But you are reading from a text file anyway, so you cannot read directly into the struct.