Alex Rivera | Logout

Looking for C# equivalent of scanf

Asked 2009-01-23T07:41:25.207
35

I used to code in C language in the past and I found the scanf function very useful. Unfortunately, there is no equivalent in C#.

I am using using it to parse semi-structured text files.

I found an interresting example of scanf implementation here. Unfortunately, it looks old and incomplete.

Does anyone know a scanf C# implementation ? Or at least something that would work as a reversed string.Format?

Edit
Report

3 Answers

8

If regular expressions aren't working for you, I've just posted a sscanf() replacement for .NET. The code can be viewed and downloaded at http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net.

answered 2011-02-25T06:17:14.010
2

I think you want the C# library functions either Parse or Convert.

// 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());
}
answered 2009-09-30T20:58:15.670
0

You could use System.IO.FileStream, and System.IO.StreamReader, and then parse from there.

answered 2009-01-23T07:58:08.357

Your Answer