Thread: Pattern matching problem with fscanf. Any suggestions?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    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:

    Code:
    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....

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

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

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about something like this?
    Code:
          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
    Posts
    3
    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, 07: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, 07:44 PM
  4. Problem with fscanf..
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 03:56 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM