Thread: Pattern matching problem with fscanf. Any suggestions?

  1. #1
    Registered User
    Join Date
    Jan 2006

    Pattern matching problem with fscanf. Any suggestions?

    Hello all. I've been trying so many combinations of this that my head has gone to jelly and i'm probably doing stupid things. Anyways. My program reads data from a file. some examples are:

    IDEN var1
    OPER +
    OPER :=
    DIGI 89
    STRN 'This is a string'

    previously this would work:

    char *first,*second;
    //some malloc stuff here
    fscanf(file, "%s %s", first, second);
    IDEN would be in first and var1 would be in second and so on.
    appart from the STRN one. it puts "STRN" in first all right, but only " 'This "in second.

    I've being trying lots of different variations of this and it either seg faults or gives useless output
    and don't forget that the solution still has to work for the previous IDEN, OPER and DIGI tags too....

    anyone have any thoughts? i'd be eternally greatful....

    Last edited by kgmf; 01-10-2006 at 12:14 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    (fscanf(file, "%s %s", first, second);
    What's that for? Is it a typo?

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2006
    whoops yes typo. sorry. tis not in my actual code

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    How about something like this?
          char line[128], first[64], second[64];
          while ( fgets(line, sizeof line, file) != NULL )
             if ( sscanf(line, "%63s %63[^\n]", first, second) == 2 )
                printf("%s %s\n", first, second);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2006
    aye....that works!
    Ta very muchly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pattern matching
    By ahahplz in forum C Programming
    Replies: 5
    Last Post: 02-07-2003, 06:15 PM
  2. C++ Compiler Problem
    By Prog.Patterson in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2002, 10:17 AM
  3. Replies: 0
    Last Post: 02-05-2002, 06:44 PM
  4. Problem with fscanf..
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 02:56 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
Website Security Test