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

If you only want to read the first line then take out that loop.

I need to store each line into the appropriate variable.

First line into Title, second line into price, third line into stock and fourth line into person.

I've been trying to use a for loop but it's not working very well.

One way to do it is to use more than one fgets()

char c; // line terminator '\n'
fgets(title, sizeof(title), pFile);
fscanf(pFile, "%f %d%c", &price, &stock,&c);
fgets(person, sizeof(person), pFile);

In addition to those you may want to remove the '\n' that fgets() puts at the end of the string.

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks for the help, my program is starting to work now.

I just have a question if you don't mind.

For "fgets(title, sizeof(title), pFile);", how does the program know when this line ends?

When I tried the code I posted earlier, it just read the entire text file.

The same way you know when a line ends - it finds the '\n' (newline) char. fgets() will have problems if the text is long, and has no newlines in it. That's odd, but I have seen text like that before.

>>That's odd, but I have seen text like that before

That could be the result of a couple problems

  1. The text file was written on a *nix or MAC computer and copied to MS-Windows without translation. MS-Windows uses two bytes (0x0A and 0x0D) to terminate lines while *nix uses just one byte (0x0D). Text files have to be run through a translator after (or during) they are transferred from one os to the other. Many file transfer programs offer that translation as an option.
  2. The file has no line terminating characters at all. In that case it is NOT a text file, but a binary file, even though it may contain text strings. In this case you will have to find out from whoever wrote the file how to read it. One way to do it which I used was to preceed the string with the string length.
This article has been dead for over six months. Start a new discussion instead.