Thread: read from txt file

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    1

    read from txt file

    hello everyone.. anyone can explain me how this code works:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    main()
    {
       FILE *fp;
       char buff[255];
    
       fp = fopen("input.txt", "r");
       if( fp != NULL ){
          while ( !feof(fp ) ){
             memset(buff, '\0', sizeof( buff) );
             fgets(buff, 255, (FILE*)fp);
             printf("%s", buff );
          }
          fclose(fp);
       }
    }
    what's feof, memset and fgets? thanks so much !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,304
    Quote Originally Posted by thejoker
    what's feof, memset and fgets?
    Opportunities for you to read up. For example, these functions might have been mentioned in the material that you are using to learn C. If you are using a Unix-like system, they are probably available via manual pages, e.g., if you run man fgets at a command prompt. They are documented online, e.g., by online versions of the aforementioned manual pages or other C related websites.

    By the way, your example's usage of feof to control a loop like that is not a good idea. Rather, the return value of fgets should be used to control the loop. The use of memset is unnecessary in this case since fgets will include the null character to terminate the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    The cast of fp in the call to fgets is both pointless and dangerous at the same time.

    It's pointless, because fp is already a FILE*
    It's dangerous, because if fp isn't a FILE*, then WTF is going to happen?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,005
    I think it is an example of poorly written code.

    Maybe you should make a list of the things done wrong in it.

    Edit added link: memset - C++ Reference

    Tim S.
    Last edited by stahta01; 03-02-2014 at 07:50 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The issue of using feof has a FAQ on this site:

    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM