Thread: fscanf

  1. #1
    Registered User
    Join Date
    Jul 2003


    couldn't find any documentation for this. This is m delima:

    I'm reading in a text file that has numbers and text. The text is on seperate lines started with a # and denote a comment line. all other lines are lines of numbers with tabs between the numbers like so

    #coment to be ignored
    12 56 78 3
    33 67 93 76
    #another comment to be ignored
    582 8782 7 23

    how do I skip over the lines with text using fscanf. I've seen syntax like fscanf(<handle> "%[^/]",buffer);
    which will read until it hits a "/" char but I need to read until I hit a number then be able to store that number in a variable. Can anyone explain how this is done?

    -- Thanks in advance Lumby

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    You really would have an easier time if you used getline or fgets to read an entire line into a string and then parse it. If the first character is '#' you can safely skip over parsing and if there's anything else, use sscanf on the string to get your numbers. Like so:
    char buff[80];
    while (fgets(buff, sizeof buff, infile) != NULL) {
      if (buff[0] == '#')
      if (sscanf(buff, "%d%d%d%d", &a, &b, &c, &d) != 4) {
        // Handle erroneous input
      else {
        // Work with your numbers
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Sandra I could kiss you!!!! This worked so well!!!!!! Thanks a ton. I would buy you a drink but I doubt you're in the Boston area!

    -- Lumby

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