-9

I have a C program that reads from keyboard, like this:

scanf("%*[ \t\n]\"%[^A-Za-z]%[^\"]\"", ps1, ps2);

For a better understanding of what this instruction does, let's split the format string as follows:

%*[ \t\n]\" => read all spaces, tabs and newlines ([ \t\n]) but not store them in any variable (hence the '*'), and will keep reading until encounter a double quote (\"), however the double quote is not input.

Once scanf() has found the double quote, reads all caracters that are not letters into ps1. This is accomplished with...

%[^A-Za-z] => input anything not an uppercase letter 'A' through 'Z' and lowercase letter 'a' through 'z'.

%[^\"]\" => read all remaining characters up to, but not including a double quote into ps2 ([^\"]) and the string must end with a double quote (\"), however the double quote is not input.

Can someone show me how to do the same thing in C++

Thank you

user7140484
  • 920
  • 6
  • 14
  • It seems that what I said it's not clear for some people! 'cin exists in C++ to take the place of scanf of C, otherwise, we don't need 'cin', 'cout' and all the features C++ introduced. – user7140484 Nov 22 '16 at 14:14
  • @AndrewHenle, so you mean I can't read from console accordding some restrictions! Is that you said? Sorry but I don't belieave. – user7140484 Nov 22 '16 at 14:16
  • @0x5453 I don't want to refer scanf in C++. I wanna do the same without cstdio – user7140484 Nov 22 '16 at 14:19
  • `std::cin` is used more for straight forward reading of words. For more advanced stuff, us [std::regex](http://en.cppreference.com/w/cpp/regex) – stefaanv Nov 22 '16 at 14:23
  • @appleapple, I read that on a book – user7140484 Nov 22 '16 at 14:28
  • Yeah, also a comment from me because it is mostly a link to std::regex. If you know regular expressions, it should be reasonable to translate, but you will need more code. – stefaanv Nov 22 '16 at 14:29
  • @stefaanv, ok, can you please write the regex for me? – user7140484 Nov 22 '16 at 14:31

1 Answers1

3

C++ supports the scanf function. There is no simple alternative, especially if you want to replicate the exact semantics of scanf() with all the quirks.

Note however that your code has several issues:

  • You do not pass the maximum number of characters to read into ps1 and ps2. Any sufficiently input sequence will cause a buffer overflow with dire consequences.

  • You could simplify the first format %*[ \t\n] with just a space in the format string. This would also allow for the case where no whitespace characters are present. As currently written, scanf() would fail and return 0 if no whitspace characters are present before the ".

  • Similarly, if no non letters or if no other characters follow before the second ", scanf would return a short count of 0 or 1 and leave one or both destination array in an indeterminate state.

For all these reasons, it would be much safer and predictable in C to first read a line of input with fgets() and use sscanf() or parse the line by hand.

In C++, you definitely want to use the std::regex package defined in <regex.h>.

chqrlie
  • 131,814
  • 10
  • 121
  • 189