Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code:

C
#include <stdio.h>

int main(void) {
  char buf[80] = "salam";
  const char *fmt = "\"%79[^\"]\"";
  int n;

  n = sscanf("\"\"", fmt, buf);
  printf("%d fields were read. buf = '%s'\n", n, buf);
  n = sscanf("\"hamidi\"", fmt, buf);
  printf("%d fields were read. buf = '%s'\n", n, buf);
}

outputs:

0 fields were read. buf = 'salam'
1 fields were read. buf = 'hamidi'

This indicates that the sscanf function can't read empty strings with the specified format string. Is there a good replacement for the regex format string to read strings with length ZERO to at most 79 characters by the sscanf function? In another words, I expect the first sscanf insert '\0' to buf[0] and return 1 instead of 0. Is there a way?

What I have tried:

This is just a question. It's not a thing that I've to try something about.
Posted

A better option would be to use fgets[^]. It includes the newline character that is read but that can be stripped off easily with a function like this :
C
void StripChar( char * buffer, int chr )
{
    char * p = strchr( buffer, chr );
    if( p )
       * p = 0;
}
Here's a small sample :
C
#define BUFFER_SIZE 79
    char buffer[ BUFFER_SIZE + 1 ] = { 0 };
    fgets( buffer, BUFFER_SIZE, stdin );
    StripChar( buffer, '\n' );
 
Share this answer
 
Comments
ilostmyid2 14-Jan-24 7:45am    
You mean scanf has no option?
Rick York 14-Jan-24 11:53am    
I really don't know but I consider fgets to be a better option to acquire text input. Once you have a line of text you can then use sscanf to extract various data from it if you want to. The format options are the same as scanf.
ilostmyid2 14-Jan-24 13:03pm    
I'm already using sscanf, not scanf. :)
The scanf() documentation explicitly says that the set extraction format (e.g. [...]) matches a non empty set of characters: std::scanf, std::fscanf, std::sscanf - cppreference.com[^]
 
Share this answer
 
v2
C++
char buf[80] = "salam";
const char *fmt = "\"%79[^\"]\"";

The actual string stored in buf contains just the characters between the two quotes, it does not include the quote characters. But since your format string tells scanf that the first character must be a quote character, the scan fails.


[edit]
C++
const char *fmt = "\"%79[^\"]\"";
int n;

n = sscanf("\"\"", fmt, buf);

As k5054 points out above, the format requires a non-empty string between the two quote characters. So the string "\"\"" will not match. Also, the messages being printed do not reflect the actual code being tested.

[/edit]
 
Share this answer
 
v2
Comments
k5054 14-Jan-24 11:31am    
Go back and check the code. buf is the target. The source is a static string.
Richard MacCutchan 14-Jan-24 11:46am    
Changing the source to "\"salam\"" will work. But as it stands the code is a bit of a mess.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900