ignoring whitespace with fscanf

This is a discussion on ignoring whitespace with fscanf within the C Programming forums, part of the General Programming Boards category; Ok, here is a short background on my problem - I'm just finishing up my C programming class, and want ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    ignoring whitespace with fscanf

    Ok, here is a short background on my problem - I'm just finishing up my C programming class, and want to use what I've learned on a project (mainly because I'm bored.) So, there is an online game called Planetarion that I play which outputs data about each player into a large text file.

    I want to write a program to search this file and output matches, etc...

    Each line consists of 3 integers, followed by two strings contained in double quotes, then 2 more integers.

    example 1:
    1 1 1 "Headquarters" "Spinner" 20 96360

    example 2: 1 1 3 "Olympus House" "Your Zeus" 3 21669

    Using the format parameters "%d %d %d %s %s %d %d" works fine for the first example, but when I come to the second example, you will notice the first string contains a whitespace which prompts C to read in the strings as "Your and Zeus", and then proceeds to read in the remaining string as integers, and so you can see this obviously is going to cause errors.

    Therefore, my question(if you don't know it already): how do I format my input so that it will read what is enclosed in double quotes, with or without whitespace.

    Thank you!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Replace each
    %s
    With
    \"%[^\"]\"

    It's called a scan-set - your C book should explain what a scan set is.

    Without all the backslashes which are needed by C, it looks like this
    "%[^"]"

    Which translates as
    A double quote, followed by a number of non-double-quote chars, and a closing double quote

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. Regaurding SetConsoleTitle() and whitespace.
    By Tronic in forum Windows Programming
    Replies: 4
    Last Post: 03-26-2004, 08:02 PM
  4. white space and fscanf
    By DMaxJ in forum C Programming
    Replies: 2
    Last Post: 06-10-2003, 09:18 AM
  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