I have some c code that works fine on a unix server (which I have come to understand is based on "System V" unix), but it blows up on my local macbook (which I have come to understand is "classic" unix).
The purpose of the code is to take a ascii file and form it into a series of structures, then rewrite it to a separate file in binary.
Code:
int main( void )
{
char filename[10];
FILE *from, *to;
char buff[100];
Class buf;
printf("\nEnter the name of your file: ");
scanf("%s", filename);
from = fopen(filename, "r");
if (!from)
printf("Error opening file\n");
else
{
sprintf(filename, "%s.bn", filename);
printf("\nCreated File\n");
to = fopen(filename, "wb");
if (!to)
printf("Error opening to file\n");
else
{
while(fgets(buff, sizeof(buff), from) != NULL)
{
if (sscanf(buff, "*format of Class struct*****, etc, etc, etc)) == 21)
{
fwrite (&buf, 1, sizeof(buf), to);
}
}
fclose (to);
}
fclose (from);
}
return 0;
}
When I run the program, I get a Bus error.
When I run the debugger, it says the problem is in the fgets function.
Code:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000038
0x9008b0e9 in fgets ()