Thread: fscanf

  1. #1

    Smile fscanf

    I've read a lot that fgets if better to use instead of scanf. My question is what about using fscanf when reading a file? Is fscanf the only way to go when reading a file?


  2. #2
    Registered User
    Join Date
    Mar 2002
    personally for me it depends what I'm doing if I need an entire line I do fgets(stream name,variable,length) (or whatever the precise syntax is), but if I'm reading a text file for records with is like a string, integer, string I'll use fscanf coz its a bit easier and I've personally never had problems, just try them both and see what ya think works best for you I reacon.

  3. #3
    OK, Thank you.

  4. #4
    According to the man page on fgets() (Solaris 5.7)

    char * fgets(char *s, int n, FILE *stream)

    The fgets() function reads characters from the stream into
    the array pointed to by s, until n-1 characters are read, or
    a newline character is read and transferred to s, or an
    end-of-file condition is encountered. The string is then
    terminated with a null character.


    Get off my Intarweb!!!!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    And for reading binary data from a file you use the fread function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Thank you all. They don't teach us how to use fgets in my C class.
    I don't understand why. Once I learned how to read a line using fgets instead of scanf it seemed so much easier and scanf seems like a pain now. LOL!
    -Thnx for the advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM
Website Security Test