4
int main()
{
  char* a = " 'Fools\' day' ";
  char* b[64];
  sscanf(a, " '%[^']s ", b);
  printf ("%s", b);
}

--> puts "Fools" in b

Obviously, I want to have "Fools' day" in b. Can I tell sscanf() not to consider escaped apostrophes as the end of the character sequence?

Thanks!

u17
  • 2,776
  • 4
  • 31
  • 43

3 Answers3

8

No. Those functions just read plain old characters. They don't interpret the contents according to any escaping rules because there's nothing to escape from — quotation marks, apostrophes, and backslashes aren't special in the input string.

You'll have to use something else to parse your string. You can write a little state machine to read the string one character at a time, keeping track of whether the previous character was a backslash. (Don't just scan to the next apostrophe and then look one character backward; if you're allowed to escape backslashes as well as apostrophes, then you could end up re-scanning all the way back to the start of the string to see whether you have an odd or even number of escape characters. Always parse strings forward, not backward.)

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
0

Replace

char* a = " 'Fools\' day' ";

with

char* a = " 'Fools' day' ";

The ' character isn't special inside a C string (although it is special within a single char). So there is not need to escape it.

Also, if all you want is "Fools' day", why put the extra 's at the start and end? Maybe you are confusing C strings with those in some other language?

Edit: As Rob Kennedy's comment says, I was assuming you are supplying the string yourself. Otherwise, see Rob's answer.

MAK
  • 26,140
  • 11
  • 55
  • 86
  • 1
    I think it's safe to assume that the input to `sscanf` won't *really* be coming from a string literal within the program. Rather, it will be read from elsewhere (such as a file, or the keyboard — hence asking about `scanf` as well) and it will contain a backslash because the apostrophe is special to the program. The program wants to read a single-quoted string that might contain single quotes. – Rob Kennedy Jul 07 '10 at 12:37
  • That is right. Sorry, I'll try to make myself more clear in the next question :) – u17 Jul 07 '10 at 13:22
0

Why on earth would you write such a thing, instead of using std::string? Since your question is tagged C++.

int main(int argc, char* argv[])
{
    std::string a = " 'Fools' day' ";
    std::string b(a.begin() + 2, std::find(a.begin() + 2, a.end(), ' '));
    std::cout << b;
    std::cin.get();
}

Edit: Oh wait a second, you want to read a string within a string? Just use escaped double quotes, e.g.

int main(int argc, char* argv[]) {
    std::string a = " \"Fool's day\" ";
    auto it = std::find(a.begin(), a.end(), '"');
    std::string b(it, std::find(it, a.end(), '"');
    std::cout << b;
}

If the user put the string in, they won't have to escape single quotes, although they would have to escape double quotes, and you'd have to make your own system for that.

Puppy
  • 144,682
  • 38
  • 256
  • 465