Originally Posted by
CommonTater
Well, there's also this in your original message...
Code:
//Retrieve and print the binary file.
void loadbin(struct Stock s[], int n)
{
FILE *f;
f = fopen("e:\final.bin", "rb");
fwrite(&s, sizeof(s[10]), n, f);
fclose(f);
}
1) you need to double thos slashes ...
2) you need to check if it actually opened or not...
3) writing to a file you're trying to read ain't gonna work...
4) size of s[10] isn't going to give you what youwant there...
Code:
void loadbin(struct Stock *s, int n)
{
FILE *f;
f = fopen("e:\\final.bin", "rb");
if (! f)
{ printf("Could not open the file");
exit(1); }
fread(s, sizeof(struct stock), n, f);
fclose(f);
}
Ya, none of that worked .
Here's the error exception:
Code:
Unhandled exception at 0x5c636af2 in cs36FINAL.exe: 0xC0000005: Access violation writing location 0x00000024.
Current Snippet:
Code:
//Save the array of structures to a text file.void savetext(struct Stock s[], int n)
{
FILE *f;
f = fopen("e:\\final.txt", "w");
int i;
for(i=0;i<n;i++)
{
fprintf(f,"%s\n",s[i].name);
fprintf(f,"%d %f %f %f %f %f %f", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
}
fclose(f);
fflush(stdin);
}
//Retrieve and print the text file.
void loadtext(struct Stock s[], int n)
{
FILE *f;
f = fopen("e:\\final.txt", "r");
int i;
for(i=0;i<n;i++)
{
fgets(s[i].name, sizeof(s[i].name), f);
fscanf(f,"%d %f %f %f %f %f %f", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
}
fclose(f);
}
//Save the array of structures to a binary file.
void savebin(struct Stock s[], int n)
{
FILE *f;
f = fopen("e:\\final.bin", "wb");
if (! f)
{ printf("Could not write to file");
exit(1); }
fread(s, sizeof(struct Stock), n, f);
fclose(f);
}
//Retrieve and print the binary file.
void loadbin(struct Stock s[], int n)
{
FILE *f;
f = fopen("e:\\final.bin", "rb");
if (! f)
{ printf("Could not open the file");
exit(1); }
fread(s, sizeof(struct Stock), n, f);
fclose(f);
}
Debugger doesn't show any errors. THe error shows up on runtime and shows to this line:
Code:
fscanf(f,"%d %f %f %f %f %f %f", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
Uggghhhh Sorry guys. I'm new and stupid with farrrrrrr less experience than probably most of you. Could you skim through the code to see if there's something complicating this issue? I even checked the perms for the files. You see it will write them to disk, but won't read them, or bugs out when it attempts (By it i mean the IDE/Debugger). I honestly don't know whats causing it....