Solved

Reading String (with whitespaces) Using SScanf

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
isEverywhere  challenge 19 82
unix example issues 18 98
Excel file not created as expected 7 79
Problem to App 4 97
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…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…

856 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