trouble understanding scanf/fscanf

Discussion in 'OT Technology' started by awns729, Nov 17, 2008.

  1. awns729

    awns729 New Member

    Joined:
    Jan 4, 2008
    Messages:
    353
    Likes Received:
    0
    Hey everyone,
    I'm working on a project and having trouble with part of it. I need to use scanf/fscanf/sscanf for it...

    I can't figure out why this code doesn't give me the output I want. I want it to input up to 18 characters for each time I read. I want it to skip/ignore any characters beyond the first 18 that on the same line and then input the first 18 characters on the next line. And then once I figure that out, I need to input even more lines up to a different number of characters. Basically all the inputs have to fit into fixed sized variable. I also prefer that all the inputs be seperated by newlines. What am I not understanding here? Not looking for just an answer, but a good explination so I can finish my program. Thanks :)

    Code:
    char *input;
     input = (char *)malloc(sizeof(char)*19);
     printf("\nEnter first name: ");
     scanf("%18[^\n]\n", input); 
    scanf("%*[^\n]\n");
     printf("\ninput: %s", input);
     resetinput(input);//sets all chars to NULL
     printf("\nEnter last name: ");
     scanf("%18[^\n]", input);
     printf("\ninput: %s", input);
    The output:
    Code:
    Enter first name: Name One here
    Testing one                   
    testing two
    input: Name One here
    Enter last name: 
    input: testing two
    It seems that before my code executes the FIRST printf, I have to input three lines, all seperated by newlines. I should only have to input teh first line, however long it may be and have upto the first 18 chars saved in the variable input. It should then clear input (the resetinput function call does this), and then it should let me do all that again.

    Please don't recommend functions that will make this easier, I can't use them.
    Edit: This seems to be working now...
    Code:
     char *input;
    // struct stud_inf temp;
     input = (char *)malloc(sizeof(char)*19);
    // scanf("%*s");
     printf("Enter first name: ");
     scanf("%18[^\n]", input);//scanf("%18[^\n]\n"
    // scanf("%*[^\n]\n");
     printf("\ninput: %s", input);
     resetinput(input);
     scanf("%*[^\n]");
     printf("\nEnter last name: ");
     scanf("\n%18[^\n]", input);
     printf("\ninput: %s", input);
    Outputs:
    Code:
    Enter first name: testing one
    input: testing one
    Enter last name: testing two
    input: testing two
    Code:
    Enter first name: test test test test test test over eighteen!
    input: test test test tes
    Enter last name: i think it workeeeeeeeeeeeeed!
    input: i think it workeee
    I kind of understand it. The first scanf reads everything up to 18 chars. The rest is left over, which the second scanf picks up. The third takes care of the newline that neither of the first two would have gotten, and then repeats. Is this correct?
     
    Last edited: Nov 17, 2008

Share This Page