Program crash :(

This program crashes at the end and i have no idea why :( I want it to transform my .txt file into a .bin file it makes the file correctly and everything but it has a fatal crash at the end. Anyone got any clue?

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
struct item{
int id,power,aim,time,value,armour,bars;
char name[101];
};
int main()
{
int a=0;
FILE *fp,*fp2;
fp=fopen("itemDB.txt","r");
fp2=fopen("itemdb.bin","wb");
struct item z;
int i,j;
char b;
for(i=0;i<39;i++)
{
fscanf(fp,"%d %d %d %d %d %d %d ",&z.id,&z.power,&z.aim,&z.time,&z.value,&z.armour,&z.bars);
b='a';
for(j=0;;j++)
{
b=fgetc(fp);
if(b=='\n')
break;
z.name[j]=b;
}
z.name[j]='\0';
fwrite(&z,sizeof(z),1,fp2);
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t",z.id,z.power,z.aim,z.time,z.value,z.armour,z.bars);
puts(z.name);
}
fclose(fp);
fclose(fp2);
return 0;

}
Anyone got any clue?

Yes, your debugger does. What does it say?
Try checking to see if the file pointers are valid before you use them.
you could try putting a condition in your j for loop. as it now stands, it could over run the allotted character buffer.

also, are you sure of the consistency of the itemDB.txt file? any irregularities in that could cause problems, including containing less than 38 lines.

the scanf family of functions are very error prone.
That code looks like C, altough it has included C++ headers like iostream, which is never used.
its c code implemented in c++ code.
Topic archived. No new replies allowed.