473,785 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scanf or sscanf

Hi all,

I've had a look at this post:
http://groups.google.com/group/micro...4ca9e25136494b

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).

Thanks.

Jul 23 '06 #1
3 8994
"ewolfman" <ew******@yahoo .comwrote:
I've had a look at this post:
http://groups.google.com/group/micro...4ca9e25136494b

But I couldn't get it to work.
Which bit couldn't you get to work? This works for me:

---8<---
using System;
using System.Runtime. InteropServices ;

class Program
{
[DllImport("crtd ll.dll",
CallingConventi on=CallingConve ntion.Cdecl,
CharSet=CharSet .Unicode,
ExactSpelling=t rue)]
static extern int swscanf(string buffer, string format,
out int value);

static void Main()
{
int value;
swscanf("42", "%d", out value);
Console.WriteLi ne(value);
}
}
--->8---

I note that scanf and related functions are not typesafe and it would be
easy to accidentally create buffer overrun problems when scanning
strings etc.
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).
The scanf function style relies heavily on passing untyped pointers,
where the type information is in the string buffer, disassociated from
the actual pointers passed.

In the interests of security and reliability, I suggest that you
consider using Regex or some other safer option instead.

-- Barry

--
http://barrkel.blogspot.com/
Jul 23 '06 #2
The scanf method call for example, does not read any input from the
user. The debugger reaches the scanf code line, and simply skips it
without pausing and waiting for the user to enter data.

So I'm using sscanf instead. This seems to work, but since .NET works
with explicit typing, I guess that there is no way I can pass multiple
parameter references (which causes me to write multiple overloads for
sscanf external declaration).

For example:
[DllImport("msvc rt.dll", CharSet = CharSet.Ansi,
CallingConventi on = CallingConventi on.Cdecl)]
public static extern int sscanf(string buffer, string format,
ref int myVar, ref int myVar2);

[DllImport("msvc rt.dll", CharSet = CharSet.Ansi,
CallingConventi on = CallingConventi on.Cdecl)]
public static extern int sscanf(string buffer, string format,
ref int myVar, ref int myVar2, StringBuilder myVar3);

Jul 26 '06 #3
Microsoft states that .NET does not support varargs. They also provide
a multiple overload external declarations for using printf. Its all
here:

http://msdn2.microsoft.com/en-us/lib...onvention.aspx

Jul 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
9875
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf. However no explanation is offered other than that scanf handels end of lines very badly. I have exeperienced such problems when doing some numerical programming but never understood it. Things like some consequitive scanfs would not read in values...
20
2922
by: Sivarn | last post by:
I'm writing a program for that takes dates as input using scanf. I want to verify that the user is inputting a full 4 digits for the year. How do I do this? I know that the return value on printf is the number of printed characters; so if I could somehow get my year variable to store the leading zeros, I could just run a check: int dummy = 0; dummy = printf("%d", year);
2
2897
by: Stu | last post by:
I have the following "C" program, which works fine. #include <stdio.h> #include <stdlib.h> #include <strings.h> int main() { char *buffer = "1234 - 5678";
4
518
by: Ivan Lam | last post by:
Hi All, Thanks for reading my post! I have a problem that using the scanf function. I would like to scan a value from a line like: file:c:\program files\mpd\mpd.exe however, when I read the value by
6
3489
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the correct output is that r = EOF. If it is taken to be a letter in the stream, then the output should be r = 0, as far as I can see. My compiler gives EOF.
185
17480
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
14
22004
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
2
5239
by: Bernard Liang | last post by:
In response, I have another question about the scanf family. After reading in a %d value, for instance, do they immediately wade through all subsequent whitespace until a non-whitespace character is reached, and then continue analysing the format string? For instance, suppose I have a buffer int num; char chr1, chr2, chr3, chr4; char my_string = "1 1 2 3 5 8abc";
26
9536
by: vid512 | last post by:
hi. i wanted to know why doesn't the scanf functions check for overflow when reading number. For example scanf("%d" on 32bit machine considers "1" and "4294967297" to be the same. I tracked to code to where the conversion itself happens. Code in scanfs just ignores return value from conversion procedures. More info in case of glibc posted here:
0
9645
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
10341
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
10155
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...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
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
6741
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
5383
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...
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.