Solved

Reading String (with whitespaces) Using SScanf

Posted on 2002-07-17
5
284 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shell script to login to remote m/c and execute commands 10 78
while loop over for loop 7 116
has77  challenge 9 97
noX challenge 17 134
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

763 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