scanf or sscanf
Collapse
This topic is closed.
X
X
-
ewolfmanTags: None
-
Barry Kelly
Re: scanf or sscanf
"ewolfman" <ewolfman@yahoo .comwrote:
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).
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
--
-
ewolfman
Re: scanf or sscanf
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);
Comment
Comment