Whitespace and scanf()

This is a discussion on Whitespace and scanf() within the C Programming forums, part of the General Programming Boards category; I need to modify a data file consisting of a table of numbers (specifically, I need to "de-modulate" each column ...

  1. #1
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189

    Whitespace and scanf()

    I need to modify a data file consisting of a table of numbers (specifically, I need to "de-modulate" each column - that is, undo a modulation operation). Unfortunately, throughout the table there are missing numbers corresponding to bad data or instrument failure. Even more unfortunately, ordinary whitespace is used for these missing points. In other words, something like this:

    Code:
    100 94   1  35  23  15
    97      -6  41  29  21
    90     -19  54      28
    88     -28  60  31  35
    89     -33  67  37  47
    I need to first read the data into a regular array of integers. Unfortunately, even when I use width format specifiers in scanf() such as "%4d", it just skips right over the whitespace, and reads data from column 3 into column 2 if there's no data in column 2. This isn't what I want - if there's whitespace in the position, I don't want the program to read anything at all into the appropriate position in the array (that is, leave the number in column 2 unchanged) but still advance to the next position in the output array so that data from column 3 ends up in column 3 where it belongs. (Even better would be reading a special character into column 2.)

    Is there any easy way to do this? Or am I going to have to read each character individually and do all the processing myself?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Read each line into a buffer using fgets

    Write a routine which splits that buffer up into fields of whatever widths you want.

    Use sscanf to read each of those fields - the return result of sscanf will tell you whether it found an integer, or just a block of white space.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stop scanf consming whitespace
    By mitchb in forum C Programming
    Replies: 2
    Last Post: 03-22-2009, 06:17 AM
  2. Second scanf keeps getting skipped.
    By yougene in forum C Programming
    Replies: 9
    Last Post: 12-23-2008, 02:39 AM
  3. Basic Scanf() help
    By DooberXL in forum C Programming
    Replies: 7
    Last Post: 10-12-2004, 12:02 AM
  4. gets VS. scanf
    By Vireyda in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2004, 09:18 AM
  5. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 08:28 AM

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