Solved

Reading String (with whitespaces) Using SScanf

Posted on 2002-07-17
5
280 Views
Last Modified: 2013-11-20

Hello

 Iam trying to read a string value from a CString object using sscanf.The CString was initially formatted using the
format function of CString.

But if the string has whitespaces in it, then I will not be able to get the
whole string with the whitespaces, if I use %s as a format specifier for that.
Even if I use %nc(n is no of characters), Iam not able to read the String
Properly?

Any ideas to read the string (with the whitespaces) from the CString?


Thanks
Sridhar
0
Comment
Question by:kesavan_sridhar
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7159478
Why aren'st you using

CString str("With whitespace");
char* psz = (LPCTSTR) str;

or

CString str("With whitespace");
char ac [ MY_SIZE];

strncpy ( ac, (LPCTSTR) str, MY_SIZE);

?
0
 

Author Comment

by:kesavan_sridhar
ID: 7159634
Actually the CString object contains lot of data including
integers and Strings, Iam extracting each one of them
by giving the Format Specifiers for them. In additon to this "String with whitespace", some other data is also there in the CString object.
Thatz why I didn't do the way that you have suggested.

Thanks
Sridhar
0
 
LVL 3

Accepted Solution

by:
Crius earned 50 total points
ID: 7159757
While you can pull out strings with white spaces using sscanf, the problem is, without any character to indicate where to stop parsing, you'll end up grabbing the whole string anyway.

What you're looking for is something you can use as a delimiter, and setting the sscanf to use that instead of the default whitespace when parsing string.

sscanf(WorkVariable, "%[^'0-9']%i %s", WorkStringWithSpaces, &WorkInt, WorkStringNoSpaces);

Notice how I used %[^'0-9'] instead of %s? What %[^'0-9'] means is, grab everything until you see something from 0 to 9. This includes spaces and carriage returns, line feeds, you name it.

Here is a small demo program I made using the above statement:

#include <stdio.h>

int main(int argc, char* argv[])
{
    char WorkVariable[]="This is my string 23 end.";
    char WorkStringWithSpaces[100], WorkStringNoSpaces[50];
    int WorkInt;

    sscanf(WorkVariable, "%[^'0-9']%i %s", WorkStringWithSpaces, &WorkInt, WorkStringNoSpaces);
    return 0;
}
0
 
LVL 3

Expert Comment

by:Crius
ID: 7159770
Oops, I forgot to mention, I ran this through in debug, and examined the variables after the sscanf executed in the variables window of MSVC++. You can print them out if you'd like. :p
0
 

Author Comment

by:kesavan_sridhar
ID: 7171188
Thank You for your response.


Bye
Sridhar
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
Need Help INsttalling wget on Mavericks OS X 3 90
Detect CR LF to each line 12 152
tripleUp challenge 7 73
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now