fscanf question..

This is a discussion on fscanf question.. within the C Programming forums, part of the General Programming Boards category; Assuming we have a text file that says: hello master ---------------- when we use fscanf(FILE,"%s",string); string just takes the hello. ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    fscanf question..

    Assuming we have a text file that says:

    hello master

    ----------------
    when we use
    fscanf(FILE,"%s",string);

    string just takes the hello. Why does it stop when it comes to the space character?

    I found a quick solution for it but I think it is foolish it is like this :

    Code:
    char x[1]=" ";
    char a[20];
    fscanf(FILE,"%s",string);
    strcat(string,x);
    fscanf(FILE,"%s",a);
    strcat(string,a);
    Last edited by ozumsafa; 07-25-2007 at 03:15 PM.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Any response?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because scanf reads strings that way.

    I think you can get round that by using the "%[ ]s" format - but I haven't tried it.

    However, maybe if what you want to read in is a long line of text with spaces and such, fgets() is the better choice?

    --
    Mats

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    fscanf reads until a whitespace is found.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    fscanf treats whitespace exactly like other members of the *scanf family do: it separates one part of a format string from another and in most cases is not part of the data. What this means is %s defines a token that starts and ends with some space.

    It's probably better to just grab the string with fgets, unless you want to tokenise the file.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But I just tested what I previously wrote, and it's not quite right, but here is a new version:
    Code:
    #include <stdio.h>
    
    void main(void) {
      char s[1000];
    
      printf("Type something:");
      scanf("%[^\n]s", s);
      printf("%s\n", s);
    }
    That does, of course, almos exactly the same thing as fgets, but without the limit checking - which is a bad thing.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    So what if I have a line like

    Tom Baldwin 95

    And If I only want to grab Tom Baldwin?

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I have some ways like using strpbrk , but there gotta be some other simple ways . Or my way is not bad?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is it guaranteed that there is a "first-name last-name number" on every line in your file?

    There are many different ways you could do it, but it all amounts to "tokenizing" and "string parsing" in one way or another. strstr, strpbrk and similar are all useful here, depending on exactly what you want to do.

    of course, you could also do:
    Code:
    char fname[100], lname[100], number;
    fscanf(file, "%s %s %d", fname, lname, &number)
    --
    Mats

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    > scanf("%[^\n]s", s);

    That is not what you think it is.

    The subset operator %[^] will read everything except the character(s) between, and stop when it arrives at one. The data read so far gets stored in a string that is hopefully big enough. Otherwise, numerous problems could arise that are much worse than the function failing.

    s in your format means that you expect an s after newlines, too, which is dubious and probably misses a lot of the time.

    You could be clever and devise something like
    Code:
    int rv;
    char line[BUFSIZ];
    rv = fscanf( "%*[^\n]", (int)sizeof line, line );
    But what about leading whitespace? Does it work as well with blank lines? Probably, but it's just so convolted to try using fscanf to read whole lines. Use fgets.
    Last edited by whiteflags; 07-25-2007 at 03:59 PM.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    This is the first time I have seen such code like this : "&#37;*[^\n]"
    That is why I can not be clever at this point I guess.

    I am now trying to figure out your clever way , but you could tell me detailed in order to pick up the pace ...
    Last edited by ozumsafa; 07-25-2007 at 04:06 PM.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by ozumsafa View Post
    This is the first time I have seen such code like this : "%*[^\n]"
    That is why I can not be clever at this point I guess.

    I am now trying to figure out your clever way , but you could tell me detailed in order to pick up the pace ...
    Bleh, I wasn't even talking to you at that point. "My way" as you put it, is not clever: I was just trying to tell mats that his format string doesn't work as well as he thinks it does. fscanf is one of those functions that I find very tough to use correctly, and the fact that people dismiss calling it so easily without thinking of the consequences ...

    I don't recommend mats approach at all. Use fgets to read the whole line, and then use functions that work on strings to tokenize the data and set up whatever variables you need. sscanf or strpbrk might work great. Try it, and if you have trouble, post some code here with your problems.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Citizen,

    I agree, and in my first post on the subject, I wrote:

    However, maybe if what you want to read in is a long line of text with spaces and such, fgets() is the better choice?
    .

    There are always dozens of different ways to do something, some are better than others, and I wouldn't personally use scanf() to read a line of text. I was just giving an example of how it _CAN_ be done.

    --
    Mats

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    That is What I was doing but , I have been trying to shorten the codes , and use different functions as few as possible. If you say strpbrk and other string functions are normal to use , that is already done then , thank you all for answers. This forum is so fast.. Good to be here. But really can anyone tell me about that "&#37;*[^\n]" thing?

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    http://msdn2.microsoft.com/en-us/lib...ch(VS.80).aspx
    http://msdn2.microsoft.com/en-us/lib...9d(VS.80).aspx
    Reading Undelimited strings

    To read strings not delimited by whitespace characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The set of characters in brackets is referred to as a control string. The corresponding input field is read up to the first character that does not appear in the control string. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

    Note that &#37;[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. Quick question about fscanf
    By zyphirr in forum C Programming
    Replies: 16
    Last Post: 11-24-2008, 09:44 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. quick fscanf question...
    By Ham in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 08:47 AM
  5. fscanf() question
    By Encrypted in forum C Programming
    Replies: 6
    Last Post: 01-20-2003, 01:14 PM

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