Thread: Very basic question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    thank you very much your help got me a very long way.

    However id hate to become a nuisance but i have another glaring problem that i cannot seem to overcome. I have another input file containing information in this fashion.

    123:Firstname:Lastname01:H:40.1:1

    the rest of the lines looking very similar to that...same format however different values. My problem is i cannot figuer out an fscanf that would copy each individual value bewteen the colons and place each into a repsective variable. I am unable to get it even close as a matter of fact and ive tried many many different variations on my idea however sadly have failed a thousand times already. If anyone knows how to get me out of my rut please help for i shall fail my class and have to pay large sums of money that i cannot afford

    anyhelp is greatly appreciated, thank you

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    sscanf is very similar to fscanf.
    Code:
    #include <stdio.h>
    
    struct record
    {
       int     i;
       char    first [ 20 ];
       char    last  [ 32 ];
       char    c;
       double  d;
       int     j;
    };
    
    int main(void)
    {
       const char text[] = "123:Firstname:Lastname01:H:40.1:1";
       struct record entry;
       if ( sscanf(text, "%d:%19[^:]:%31[^:]:%c:%lf:%d",
                   &entry.i, entry.first, entry.last,
                   &entry.c, &entry.d, &entry.j) == 6 )
       {
          printf("entry.i     = %d\n", entry.i);
          printf("entry.first = %s\n", entry.first);
          printf("entry.last  = %s\n", entry.last);
          printf("entry.c     = %c\n", entry.c);
          printf("entry.d     = %f\n", entry.d);
          printf("entry.j     = %d\n", entry.j);
       }
       return 0;
    }
    
    /* my output
    entry.i     = 123
    entry.first = Firstname
    entry.last  = Lastname01
    entry.c     = H
    entry.d     = 40.100000
    entry.j     = 1
    */
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM