fscanf

This is a discussion on fscanf within the C++ Programming forums, part of the General Programming Boards category; couldn't find any documentation for this. This is m delima: I'm reading in a text file that has numbers and ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    fscanf

    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
    Posts
    9,796
    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:
    Code:
    char buff[80];
    
    while (fgets(buff, sizeof buff, infile) != NULL) {
      if (buff[0] == '#')
        continue;
      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
    Posts
    4
    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-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