Thread: reading a text in a file

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    38

    reading a text in a file

    this is the code but i have some questions on it !!?



    Code:
    #include <stdio.h>#include <string.h>
    #define LINE_SIZE 80 /* # characters per line. */
    #define SCREEN_LINES 24 /* # lines per screen. */
    /* This function displays the contents of a text file
    * one screenful at a time.
    * It returns 0 if successful and -1 otherwise.
    */
    int DisplayFile(char FileName[])
    {
    FILE *fp;
    char LineBuffer[LINE_SIZE], *ptr;
    int LineCount = 0;
    fp = fopen(FileName, "r");
    if (fp == NULL) {
    printf("File '%s' not found.\n", FileName);
    return -1;
    }
    /* Display the text one screen at a time */
    do {
    /* Read and display a single line */
    ptr = fgets(LineBuffer, LINE_SIZE, fp);
    if (ptr != NULL) {
    printf("%s", LineBuffer); /* Display the line */
    LineCount++;
    }
    /* Have we displayed a screenful yet (or finished) ? */
    if (LineCount == (SCREEN_LINES - 1) || ptr == NULL) {
    printf("\n\t\tPress RETURN to continue");
    /* Wait for user to press ENTER before continuing */
    while (getchar() != '\n')
    ; /* do nothing */
    LineCount = 0; /* reset counter */
    }
    } while (ptr != NULL);
    return 0;
    }
    int main(void)
    {
    int result;
    result = DisplayFile("sample.txt");
    return result;
    }

    i dont get this part ???????????

    Code:
    do {/* Read and display a single line */
    ptr = fgets(LineBuffer, LINE_SIZE, fp);
    if (ptr != NULL) {
    printf("%s", LineBuffer); /* Display the line */
    LineCount++;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fgets() puts input from the stream pointed to by ptr, into the char array linebuffer[], up to the size of the variable in the middle of the call to fgets(). Input terminates when a newline char is found, so it's pulling in one line of text, with each call to fgets().

    When it reaches the end of the file, it will return NULL to the ptr.

    This is a great way to get sting input from the user, also. Just replace the fp in there, with stdin.

    Try it, but unlike scanf():

    1) fgets adds the newline to the string, and the end of string marker char: '\0', which you will not see if you print out the string (you might notice the cursor moving as the newline is printed, though). So if you enter a name: Bob, and compare it with a list of names, it won't compare equal to "Bob".

    fgets() has entered "Bob" as "Bob\n". To change Bob\n back to Bob, you need to:

    Code:
    if(linebuffer[len-1]=='\n')
       linebuffer[len-1]='\0';  //overwriting the newline fgets added
    Now Bob will compare equal to the Bob from the user, using the standard strcmp(string1, string2) method. Strings can't be compared with an == sign, in C.

    2) fgets is size safe. The user can't overflow the buffer and hijack your program, by buffer overrun with fgets(). If the user tries, the extra input will be truncated (cut off).
    Last edited by Adak; 10-12-2011 at 09:29 AM.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    do {/* Read and display a single line */
    ptr = fgets(LineBuffer, LINE_SIZE, fp);
    if (ptr != NULL) {
    printf("%s", LineBuffer); /* Display the line */
    LineCount++;
    }
    This code is essentially saying:

    read a line from file fp, store it in LineBuffer, with a size of LINE_SIZE (80)
    if successful, print LineBuffer to stdout, and increment LineCount


    Read the man page for fgets. man page fgets section 3
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    Thank you Adak and Babkockdood. That makes a lot of sense ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading From a text file
    By jian in forum C Programming
    Replies: 6
    Last Post: 12-31-2010, 04:43 AM
  2. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  3. reading text from a file
    By gustavson in forum C Programming
    Replies: 1
    Last Post: 10-26-2007, 01:18 AM
  4. Reading text file twice, can it be done once?
    By CaeZaR in forum C Programming
    Replies: 2
    Last Post: 02-03-2006, 03:34 PM
  5. Reading text file
    By pabellon in forum C Programming
    Replies: 1
    Last Post: 12-19-2001, 01:09 PM