fscanf

This is a discussion on fscanf within the C Programming forums, part of the General Programming Boards category; I've read a lot that fgets if better to use instead of scanf. My question is what about using fscanf ...

  1. #1
    Unregistered
    Guest

    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?

    -Thnx

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    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
    Unregistered
    Guest
    OK, Thank you.

  4. #4
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    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.

    ...
    DrakkenKorin

    Get off my Intarweb!!!!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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
    Unregistered
    Guest
    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-29-2008, 12:11 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 11:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 02:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 02:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21