题
我用代码C语言在过去我发现的 scanf
功能非常有用的。不幸的是,没有等效的。
我使用的是利用它来分析的半结构化的文本文件。
我发现了一个interresting例 scanf
执行情况 在这里,.不幸的是,它看起来旧和不完整的。
任何人都不会知道 scanf
C#执行?或至少东西,将工作作为一种颠倒的 string.Format
?
解决方案
如果定期表达的不是为你工作,我刚刚发布 sscanf()
替换。网。代码可以查看和下载 http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net.
其他提示
由于文件是"半结构化"不能使用一个组合ReadLine()和TryParse()方法,或Regex类来分析你的数据?
你可以使用scanf直接从C runtime库,但这可能是困难的,如果你需要运用不同参数计数。我建议你经常表达对你的任务或描述,在这里的任务,也许还有另一种方式。
我找到一个更好的解决办法比使用sscanf从C或一些重写的一部分人(没有犯罪行为)
http://msdn.microsoft.com/en-us/library/63ew9az0.aspx 看看这篇文章,说明了如何使命名的团体中提取的数据从一个图案串。当心点小误差在文章和更好的版本如下。(结肠是不是该集团的一部分)
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string url = "http://www.contoso.com:8080/letters/readme.html";
Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",RegexOptions.None, TimeSpan.FromMilliseconds(150));
Match m = r.Match(url);
if (m.Success)
Console.WriteLine(r.Match(url).Result("${proto}:${port}"));
}
}
// The example displays the following output:
// http::8080
试图进口msvcrt.dll
using System.Runtime.InteropServices;
namespace Sample
{
class Program
{
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int printf(string format, __arglist);
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int scanf(string format, __arglist);
static void Main()
{
int a, b;
scanf("%d%d", __arglist(out a, out b));
printf("The sum of %d and %d is %d.\n", __arglist(a, b, a + b));
}
}
}
效果很好。净框架。但在单,它显示错误信息:
Unhandled Exception:
System.InvalidProgramException: Invalid IL code in Sample.Program:Main (): IL_0009: call 0x0a000001
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in Sample.Program:Main (): IL_0009: call 0x0a000001
如果你需要单兼容性,需要避免使用 arglist
using System.Runtime.InteropServices;
namespace Sample
{
class Program
{
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int printf(string format, int a, int b, int c);
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int scanf(string format, out int a, out int b);
static void Main()
{
int a, b;
scanf("%d%d", out a, out b);
printf("The sum of %d and %d is %d.\n", a, b, a + b);
}
}
}
在这些论点是固定的。
编辑2018-5-24
arglist
不会的工作。净的核心。它似乎是叫C vararg功能已被否决。你应该使用。净串API如 String.Format
代替。
有很好的理由为什么一个scanf样的功能缺失。这是非常容易出现误差和不灵活。使用Regex更多灵活的和强有力的。
另一个好处是,它更容易重复使用在你的代码如果你需要分析同样的事情在不同地区的代码。
我觉得你想要C#图书馆职能分析或转换。
// here's an example of getting the hex value from a command line
// program.exe 0x00080000
static void Main(string[] args)
{
int value = Convert.ToInt32(args[1].Substring(2), 16);
Console.Out.WriteLine("Value is: " + value.ToString());
}
你可以使用的系统。IO.文件流和系统。IO.StreamReader,然后分析从那里。
虽然这看起来有点粗它得到完成的工作:
// Create the stream reader
sr = new StreamReader("myFile.txt");
// Read it
srRec = sr.ReadLine();
// Replace multiple spaces with one space
String rec1 = srRec.Replace(" ", " ");
String rec2 = rec1.Replace(" ", " ");
String rec3 = rec2.Replace(" ", " ");
String rec4 = rec3.Replace(" ", " ");
String rec5 = rec4.Replace(" ", " ");
String rec6 = rec5.Replace(" ", " ");
String rec7 = rec6.Replace(" ", " ");
String rec8 = rec7.Replace(" ", " ");
String rec9 = rec8.Replace(" ", " ");
// Finally split the string into an array of strings with Split
String[] srVals = rec9.Split(' ');
然后可以使用的阵srVals作为单独变量的记录。