Thread: issue reading in text

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    20

    issue reading in text

    I'm trying to read in lines of text from a file. The line will be formatted in one of three ways. It will either be just a character, a character and one string or a character and two strings. Everything is separated by white space. I am using the command:
    Code:
    int tmp = fscanf(in, " %c %79[^\n]s %79[^\n]s", &code, arg2, arg3);
    I am having an issue when there are three lines with only a single character, it is read in as a single character. Also there is an issue when there is only a character and one string on the line. Can I use a single fscanf() command to read in line of text like this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ^\n means "read everything except a newline". You should just use fgets and then do sscanf:
    Code:
    howmany = sscanf( buf, "%c %s %s", &letter, buf1, buf2 );
    Check to see howmany things you just read.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    " %c %79[^\n]s %79[^\n]s"
    Depending on your platform, that still will not work even when fixed by adding the other spacing characters to the ignore list. So, don't go around trying to fix it, just grab the line and parse from there as quzah said.

    [Edit]
    A remarkable number of implementations will freak out with multiple uses of the set option in the same call to `scanf', but break the uses into multiple calls to `scanf' and you'll be fine.
    [/Edit]

    [Edit]
    Are you up for the suck?
    Still freaks me out a little...
    [/Edit]

    Soma
    Last edited by phantomotap; 04-25-2011 at 09:25 PM. Reason: none of your options

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL_ttf & OpenGL - text color issue
    By illizit in forum Game Programming
    Replies: 1
    Last Post: 11-02-2010, 11:56 AM
  2. Reading Lines from File Issue (integers and character strings)
    By kbfirebreather in forum C Programming
    Replies: 4
    Last Post: 10-17-2009, 02:02 PM
  3. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 04:38 AM
  4. Reading File Issue
    By dr0be in forum C Programming
    Replies: 23
    Last Post: 05-06-2007, 09:41 AM
  5. Separate text when reading text
    By 3saul in forum C Programming
    Replies: 1
    Last Post: 07-03-2006, 07:40 PM