Can someone please show me an example of how to read lines from a file with this format:
Book One
49.99
1
Authors Name
I know I am supposed to use fgets since there is whitespace in the strings, i'm just not sure how to read and store the data from different lines.
This is what I have so far:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* pFile = NULL;
char data[255];
char filename[80];
char title[100];
float price = 0;
int stock = 0;
char person[100];
// asking the user to enter a filename, the file will read the conents
printf("Please enter a filename for input: ");
scanf("%s", &filename);
pFile = fopen(filename, "r");
if (pFile == NULL)
{
printf ("Error opening file!\n");
return -1;
}
while (!feof(pFile))
{
fgets(title, 255, pFile);
printf("%s \n", title);
}
fclose(pFile);
getch();
return 0;
}
When I run this program, it stores the entire file, when I just want to save the first line to char title.
Please help