Thread: help with scanf given a file as input

  1. #1
    Registered User
    Join Date
    Apr 2011

    help with scanf given a file as input

    for this program I'm writing I need to take input from a file and draw a fractal image based on the Lindenmayer System. The input looks a little bit like this:

    a -> FFFFFy[++++n][----t]fb
    b -> +FFFFFy[++++n][----t]fc
    c -> FFFFFy[++++n][----t]fd
    d -> -FFFFFy[++++n][----t]fe
    e -> FFFFFy[++++n][----t]fg
    g -> FFFFFy[+++fa]fh
    h -> FFFFFy[++++n][----t]fi
    i -> +FFFFFy[++++n][----t]fj
    j -> FFFFFy[++++n][----t]fk
    k -> -FFFFFy[++++n][----t]fl
    l -> FFFFFy[++++n][----t]fm
    m -> FFFFFy[---fa]fa
    n -> ofFFF
    o -> fFFFp
    p -> fFFF[-s]q
    q -> fFFF[-s]r
    r -> fFFF[-s]
    s -> fFfF
    t -> ufFFF
    u -> fFFFv
    v -> fFFF[+s]w
    w -> fFFF[+s]x
    x -> fFFF[+s]
    y -> Fy

    I am trying to get the values using scanf :

    int edf, depth, numRules, numImages;
    double angle, initHead;
    char *lhs[100], *rhs[100], *axiom; //lhs and rhs are for storing the rules and not used here
    scanf("%d %d %lf %lf %c", &numImages, &edf, &angle, &initHead, &axiom);
    currently I'm trying to grab "aF" by initializing "axom" as a string (I included <string.h> in the header) and assigning the 5th value in the file to it. However I know that there can be issues using a string in scanf.

    I've tried alternatively tried making axiom an array of chars but that just grabs everything else in the file until the array is filled and the length of "axiom" will vary by test file.

    The only other way I can currently think of involves storing the whole file to an array of chars and iterating through one by one. Is there an easier way to do this or am I just over-thinking things?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well to grab "af", you would need

    char axiom[3];  // 2 chars max
    scanf("%d %d %lf %lf %s", &numImages, &edf, &angle, &initHead, axiom);  // %s format, and no &
    > a -> FFFFFy[++++n][----t]fb
    I would suggest you use fgets() to read these lines into a char array, then write your own custom parser to go over the line looking for the bits of information you need.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fraction Input with Scanf
    By daniellopez in forum C Programming
    Replies: 1
    Last Post: 01-30-2011, 12:23 AM
  2. input using scanf()
    By kizyle502 in forum C Programming
    Replies: 5
    Last Post: 09-10-2009, 12:56 AM
  3. Input and Scanf
    By DAaaMan64 in forum C Programming
    Replies: 3
    Last Post: 01-14-2009, 06:56 PM
  4. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 03:54 PM
  5. help with input and scanf
    By kpw in forum C Programming
    Replies: 9
    Last Post: 08-28-2001, 08:21 PM

Tags for this Thread