473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problems doing sscanf() on strings

I'm having following trouble:

I'm reading contents of a string retrieved from a CSV-like file with sscanf, in following way:

Expand|Select|Wrap|Line Numbers
  1. sscanf( originalString, "%d,%s,%s", &myInteger, myString1, myString2 );
originalString contents are something like:

Expand|Select|Wrap|Line Numbers
  1. char originalString[]="1,my text here 1,my text here  2";
I'm correctly getting myInteger, but instead of getting "my text here 1" and "my text here 2" in myString1 and myString2 respectively, I'm getting a non-null terminated string in myString1, which contains both textes. myString2 becomes unmodified in sscanf.

I guess that sscanf can't distinguish the comma while it is reading a %s string, and I don't know why it is not placing a '\0' (%s should tell it I want to get a string, not an array of chars).

Does anybody know how to workaround this issue, according to my input string originalString[]?
Jun 19 '08 #1
2 3950
gpraghuram
1,275 Expert 1GB
The problem is using , along with format specifiers..
Try to tokenize the string using strtok and get the strings separately.

Raghuram
Jun 19 '08 #2
Thanks for your answer,

I've parallelly looked for a solution:

First, some comments on my post.

In first place:

My code in the post was not really the one I was testing, and it affects the result. The thing is that, according to [http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#scanf], any whitespace char (like space) is considered a separator when you do a scanf on "%s", so it will never be taken into "%s"

The solution has been, as you suggest, parsing the string in a different way. Instead of using strtok (which has a pretty weird output), i've parsed the string myself (yes, I know it won't be as optimal as using asm-coded string.a files, but maybe more clear to me). I paste here the function I wrote, tested working OK, just if becomes useful to anybody:

Expand|Select|Wrap|Line Numbers
  1. T_s32
  2. getStringFromCsvLine( T_u8 *csvLine, T_u8 *outputString, T_u32 fieldNo, T_u8 separator )
  3. {
  4.     T_u32    lineLength;
  5.     T_u32    i_u32, j_u32, k_u32;
  6.     T_u32    fieldFound;
  7.  
  8.     lineLength = strlen( csvLine );
  9.  
  10.         // reset field counter
  11.     j_u32 = 0;
  12.  
  13.         // reset current char in output string being filled
  14.     k_u32 = 0;
  15.  
  16.     fieldFound = 0;
  17.  
  18.     for( i_u32 = 0;
  19.         i_u32 < lineLength;
  20.         i_u32++ )
  21.     {
  22.         if( csvLine[ i_u32 ] == separator )
  23.         {
  24.             j_u32++;
  25.             continue;
  26.         }
  27.  
  28.         if( j_u32 == fieldNo )
  29.         {
  30.             fieldFound = 1;
  31.  
  32.             outputString[ k_u32 ] = csvLine[ i_u32 ];
  33.  
  34.             k_u32++;
  35.         }
  36.     }
  37.  
  38.     if( fieldFound )
  39.     {
  40.         outputString[ k_u32 ] = '\0';
  41.         return 0;
  42.     }
  43.  
  44.     return 1;
  45. }
Of course any comments or suggestions on this code are welcome.

Thanks again.-
Jun 19 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
2
by: Bryan Feeney | last post by:
My life was full and happy lately, which just wan't right, so I've ended up trying to implement an encryption algorithm through JavaScript! However the output I'm getting doesn't match the...
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
11
by: John E Katich | last post by:
Being an old MFC guy, I'm having a heck of a time working with MC++. Can anybody tell me why the following code outputs 65 instead of an A. And yes I know 65 is the hex value for A, but why is...
0
by: jd | last post by:
I am trying to figure out how to deploy a page and I'm not making much progress. I created an IIS virtual directory, configured it for ASP.NET 2, turned off authentication, and then copied a...
3
by: ewolfman | last post by:
Hi all, I've had a look at this post:...
1
by: nass | last post by:
hello everyone.. i am not sure what is wrong - never liked strings anyway! my problem is that i am trying to save a QCstring in a shared memory segment and i get a segmentation fault.. here is...
2
by: Simon | last post by:
I have problems with connection strings. I have added new connection string to Web.config: <connectionStrings> <add name="ConnStr" connectionString="xxxxxxxxxxxxxxx"/> </connectionStrings> ...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.