Solved

Reading String (with whitespaces) Using SScanf

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

705 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

12 Experts available now in Live!

Get 1:1 Help Now