Thread: scanf()

  1. #16
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by laserlight View Post
    What does [^\n] mean?
    ...keep receiving characters until newline (enter key) is hit.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    ...keep receiving characters until newline (enter key) is hit.
    Okay. Why does this statement terminate as soon as enter key is hit?
    Code:
    scanf("%[^\n]s %[^\n]s",str1,str2);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Okay.. But aren't there two format strings. So isn't it supposed to keep receiving characters until enter is hit twice??

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    Okay.. But aren't there two format strings.
    There are two format specifiers in a single format string.

    Quote Originally Posted by juice
    So isn't it supposed to keep receiving characters until enter is hit twice??
    The problem is probably that the newline is left in the input buffer after matching with the first format specifier, then it is used to match with the second format specifier. Anyway, you could just break the format string into two parts, i.e., use two scanf calls.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I tried this statement and it worked. The only difference is that I removed the letter 's' from the format specifier...Why so???

    Code:
    scanf("%[^\n] %[^\n]",str1,str2);

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If we could give %[^] a name, it would be a scan set.

    This string is a scan set all by itself:
    "%[^\n]"

    Now "%[^\n]s" is a scan set and the letter s, because every letter stands for itself, per the rules you learned. So %[^]s is not a special thing by itself.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    I tried this statement and it worked.
    Out of curiosity, but why did you try that statement?

    Quote Originally Posted by juice
    The only difference is that I removed the letter 's' from the format specifier...Why so?
    What do you think?

    Look, you have access to documentation too. What does it say? I am starting to suspect that I have been wrong in taking examples that I have seen at face value, but rather than jump the gun and tell you my reasoning, I want to hear yours.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    This is the code that I was writing, it is supposed to receive data for the structure blood and dump it into the file pointed by fp. But the loop terminates after the first run itself..

    Code:
    #include<stdio.h>
    
    int main()
    {
    	struct blood
    	{
    		char name[21];
    		char address[41];
    		int age, bt;
    	}donor;
    	FILE *fp;
    
    	fp=fopen("acc.txt","w");
    
    	puts("Enter data");
    
    	while(scanf("%[^\n] %[^\n] %d %d",donor.name,donor.address,&donor.age,&donor.bt) == 4)
    		fprintf(fp,"%s %s %d %d\n",donor.name,donor.address,donor.age,donor.bt);
    
    }

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's because scanf sucks.

  10. #25
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There's virtually no reason to use scanf, to name a few:

    - Dealing with newline characters is painful, whereas a fgets() call does not try to format it for you
    - It's horrible for reading strings, as you cannot set the size limit which leads to segfaults, vulnerabilities in your code, or wasted memory
    - It encourages programmers to be lazy by letting them read incredibly specific format strings (lets say you wanted a comma-delimited list of ints), without providing good error checking. In the case of reading a comma-delimited list of ints, scanf is ignoring the possibility that the user is entering invalid values, whereas splitting the string by commas, removing spaces, and using the if(!(num = atoi(chr))) syntax; is much easier to check for errors in the input

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with scanf...
    By vsovereign in forum C Programming
    Replies: 7
    Last Post: 03-01-2010, 11:29 AM
  2. gets? scanf?
    By msshapira in forum C Programming
    Replies: 4
    Last Post: 01-07-2009, 03:53 PM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. Help with scanf
    By newprog in forum C Programming
    Replies: 6
    Last Post: 01-29-2003, 09:12 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM