Thread: ignoring whitespace with fscanf

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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-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. Regaurding SetConsoleTitle() and whitespace.
    By Tronic in forum Windows Programming
    Replies: 4
    Last Post: 03-26-2004, 09: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