I'm having following trouble:
I'm reading contents of a string retrieved from a CSV-like file with sscanf, in following way: - sscanf( originalString, "%d,%s,%s", &myInteger, myString1, myString2 );
originalString contents are something like: - 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[]?
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
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: - T_s32
-
getStringFromCsvLine( T_u8 *csvLine, T_u8 *outputString, T_u32 fieldNo, T_u8 separator )
-
{
-
T_u32 lineLength;
-
T_u32 i_u32, j_u32, k_u32;
-
T_u32 fieldFound;
-
-
lineLength = strlen( csvLine );
-
-
// reset field counter
-
j_u32 = 0;
-
-
// reset current char in output string being filled
-
k_u32 = 0;
-
-
fieldFound = 0;
-
-
for( i_u32 = 0;
-
i_u32 < lineLength;
-
i_u32++ )
-
{
-
if( csvLine[ i_u32 ] == separator )
-
{
-
j_u32++;
-
continue;
-
}
-
-
if( j_u32 == fieldNo )
-
{
-
fieldFound = 1;
-
-
outputString[ k_u32 ] = csvLine[ i_u32 ];
-
-
k_u32++;
-
}
-
}
-
-
if( fieldFound )
-
{
-
outputString[ k_u32 ] = '\0';
-
return 0;
-
}
-
-
return 1;
-
}
Of course any comments or suggestions on this code are welcome.
Thanks again.-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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
|
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);
|
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. ...
| |
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).
|
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...
|
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;
|
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....
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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();...
| |
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...
| |