The program runs and can successfully write to a text file, but when I try to read back from the text file I get:
"Exception thrown at 0x0F8C7071 (ucrtbased.dll) in Text Files Homework.exe: 0xC0000005: Access violation writing location 0xFFFFFFCC."
Which brings me to line 1092 in stdio.h
.
The code inside the function that is supposed to read each line is: Product temp;
char name[MAX_STRING_LEN];
char type;
double price;
int qty;
int moreData = fscanf_s(stream, "%s", temp.name, MAX_STRING_LEN - 1);
if (moreData != EOF)
{
fscanf_s(stream, "%c", temp.type);
fscanf_s(stream, "%lf", &temp.price);
fscanf_s(stream, "%d", &temp.qty);
// echo what just got read (from the file) to the console.
printf_s("%s %c %.2f %d was read \n\n", temp.name, temp.type, temp.price, temp.qty);
}
else
{
printf("Read past end of file.\n\n");
}
*pro = temp;
return moreData;
And the function that calls that function is:
FILE *stream;
Product p;
*numProducts = 0;
int err = fopen_s(&stream, fileName, "r");
if (err)
printf_s("File %s was not opened for reading. \n", fileName);
else {
printf("\n\nReading Product data from file... \n");
// Read data back from file:
int moreData = readLine(stream, &p);
while (moreData != EOF) {
printf("Number of products was %d ", *numProducts);
reportLine(p);
list[*numProducts] = p;
*numProducts = *numProducts + 1;
printf("It is now incremented to %d \n", *numProducts);
moreData = readLine(stream, &p);
}
fclose(stream);
}