Thread: Reading and printing a file

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    Reading and printing a text file

    I'm finishing up a schoolproject in C.

    I was doing a helpfunction, wich should print information onto the console, the information is in a .txt file

    Code:
    #include <conio2.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void help(void) {
        char line[80];
        FILE *help;
    
        help = fopen ("help.txt", "r");
        if (help == NULL)
        {
            printf("help.txt kan niet geopend worden!");
        }
        do {
            fscanf(help,"%[^/n]",line);
            printf("%s/n",line;
        } while (!feof(help));
    
        fclose(help);
    
        getch();
    }
    the function seems to be in a non-stop loop, printing only the first part of the txt-file
    if I put only a few words into it, it works fine, but when I put a whole text in the file, it goes crazy
    Last edited by T1m; 01-08-2009 at 01:21 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If help is NULL, return.
    Avoid using feof() as a loop condition: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Use fgets instead of fscanf.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  3. reading and printing
    By Rakansen in forum C Programming
    Replies: 11
    Last Post: 10-26-2006, 02:46 PM
  4. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM