سؤال

كنت البرمجية في لغة C في الماضي ووجدت scanf وظيفة مفيدة جدا.للأسف, لا يوجد تعادل في C#.

أنا باستخدام استخدامه لتحليل شبه منظم ملفات نصية.

لقد وجدت إينتيريستينج سبيل المثال من scanf تنفيذ هنا.للأسف, يبدو من العمر وغير مكتملة.

لا أحد يعرف scanf C# التنفيذ ؟ أو على الأقل شيء من شأنه أن يعمل كما عكس string.Format?

هل كانت مفيدة؟

المحلول

إذا التعابير العادية لا تعمل بالنسبة لك، لقد نشرت مجرد استبدال sscanf() ل.NET. ويمكن الاطلاع على رمز وتحميلها على HTTP: //www.blackbeltcoder كوم / مقالات / سلاسل / أ-sscanf استبدال مقابل صافي .

نصائح أخرى

ومنذ الملفات هي "شبه منظم" لا يمكنك استخدام مزيج من ريدلاين () وTryParse () طرق، أو الطبقة التعبيرات المنتظمة لتحليل البيانات الخاصة بك؟

ويمكنك استخدام scanf مباشرة من مكتبات C وقت التشغيل، ولكن هذا يمكن أن يكون صعبا إذا كنت بحاجة لتشغيله مع عد معلمات مختلفة. أنصحك أن التعابير العادية بالنسبة لك مهمة أو وصف تلك المهمة هنا، ربما هناك طرق أخرى.

ولقد وجدت حلا أفضل من استخدام 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));
        }
    }
}

الذي يعمل بشكل جيد على .NET Framework.ولكن على أحادية ، فإنه يظهر رسالة الخطأ:

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 لا يعمل على .صافي الأساسية سواء.ويبدو أن استدعاء ج vararg وظيفة هو مستنكر.يجب عليك استخدام .صافي سلسلة API مثل String.Format بدلا من ذلك.

وهناك سبب وجيه لماذا scanf مثل وظيفة مفقودة من ج #. انها عرضة جدا للخطأ، وليس مرنا. باستخدام التعبيرات المنتظمة هي أكثر مرونة وقوة.

وفائدة أخرى هي أنه من الأسهل لإعادة طوال التعليمات البرمجية الخاصة بك إذا كنت بحاجة إلى تحليل الشيء نفسه في أجزاء مختلفة من التعليمات البرمجية.

وأعتقد أنك تريد وظائف 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());
}

هل يمكن استخدام System.IO.FileStream، وSystem.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 مجموعة كمتغيرات فردية من السجل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top