quick fscanf question...

This is a discussion on quick fscanf question... within the C++ Programming forums, part of the General Programming Boards category; How do you use fscanf to read in a string into a character array, say buf, INCLUDING the spaces???...

  1. #1
    Ham
    Ham is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    14

    quick fscanf question...

    How do you use fscanf to read in a string into a character array, say buf, INCLUDING the spaces???

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    fscanf( myfile, "%[^|]", buf);

    Although fgets is often more suited to the task of line input.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Ham
    Ham is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    My input file is:

    0
    0
    3
    0
    +9999999999
    +8000000001
    +1001002003
    -8003000000
    +9999999999


    My code is:

    FILE *fp = fopen("test.ail", "r");

    char buf[18];

    while(1)
    {

    //fscanf( fp, "%[^|]", buf);

    fgets (buf, 18, fp);
    printf ("\n%s", buf);

    if ( (strcmp(buf, "+9999999999")) == 0)
    break;
    }

    ...but for some reason, the program keeps printing out a whole line of space between each number like so for the output:

    0

    0

    3

    0

    +9999999999

    +8000000001

    +1001002003

    -8003000000

    +9999999999



    How can I fix this?

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Perhaps you're storing the newline / CR character from your file in the string. When you combine that with the "\n" in your printf statement you get 2 line feeds instead of one.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>How do you use fscanf to read in a string into a character array, say buf, INCLUDING the spaces???
    You use something better suited to line input :-)
    Code:
    cin.getline(buf, delim);
    fscanf really should be used only for specific formatting, if you ever need to read a whole line then use a function designed for that job, it's safer and more natural. :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21