Thread: File I/O

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    15

    File I/O

    Hi all. I am having trouble with file I/O.
    Let say I have an input file like this
    x1 y2 b4
    85 z4 x2
    I'm trying to read the input without using arrays. I would use fscanf(*arguments*) to get the first char, which is x. Then I call it again to get the second one, which is 1. Now I would need to get the second input, y2. How do I skip the white space character to get to char y ?
    And is it an effective way to get all inputs though ? (each input, example x1, is read into a function). Thanks in advance everyone

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you want to read character by character you should use fgetc.
    To skip the whitespace, just read it as a character, check that it's whitespace, and move on.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    fscanf(fin," %c",&ch);
    will read a file one non-whitespace character at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    thank you. That's what I did. Just a bug that didn't make the program run as expected

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-06-2012, 02:46 PM
  2. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  3. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  4. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM