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,674
    And for reading binary data from a file you use the fread function.
    I used to be an adventurer like you... then I took an arrow to the knee.

  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-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

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