Solved

Reading String (with whitespaces) Using SScanf

Posted on 2002-07-17
5
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

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