473,770 Members | 4,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems doing sscanf() on strings

86 New Member
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 3985
gpraghuram
1,275 Recognized Expert Top Contributor
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
jorba101
86 New Member
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.ed u/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
2516
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) for my pupils. This very simple thing does not seem simple at all. If I use strings, I cannot replace their parts (though I can use string.find for the searching). I think it is a bad idea that strings are not mutable, but I suspect that...
2
1577
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 reference C program I created. The input is a sequence of 32-bit integers, a 128 bit key (supplied as four 32-bit integers) and the number of passes. A separate function takes care of converting strings to these integers - four characters per 32-bit...
4
4259
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
1545
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 it getting converted? And how to I get an A to be outputted? char c = 'A'; StringBuilder *s = new StringBuilder; s->Append(c); Console::WriteLine(s);
0
850
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 trivial page (see below) to the virtual directory. I get an error "Object reference not set to an instance of an object" trying to reference Request.Params. If I run under the Visual Studio 2005 built-in Web server, this works without a problem. ...
3
8992
by: ewolfman | last post by:
Hi all, I've had a look at this post: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/a5bec880d8efbb92/184ca9e25136494b?lnk=st&q=&rnum=1&hl=en#184ca9e25136494b But I couldn't get it to work. In short, how can I call scanf or sscanf directly from C# (i.e. I'd rather have a DllImport and perform the call directly as I would in C++, instead of using custom classes).
1
2450
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 the case statement that causes the trouble.. i could post the whole procedure but there is no point. similar case statements as the following one exisst in the procedure that save in the same memory segment (but at dif offest) floats, Q_UINT8 and...
2
1768
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> Bu when I want to access the data with: coll = ConfigurationManager.ConnectionStrings;
6
3884
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 definined as taking a char * as an argument? For example the C++ header says SDCERR GetCurrentConfig(DWORD *num, char *name); I am using Uint for the *num parameter, which returns the correct value but for *name, I always get back a string of 6 squares....
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.