2017-07-30 78 views
-1

我是C#編程的新手,我有點不知所措,我可以使用所有工具,但找不到解決問題的工具。在文本文件中搜索一個短語並在C中讀取下一個單詞#

我有一個包含大量信息的文本文件。我需要找到「[PRG]」短語並讀取緊跟在該短語後面的索引號,並且在下面的行中,我需要跳過一個單詞並閱讀該行的其餘部分。

這裏是文本文件的樣本:

[TYP] 1001 1
[PRG] 0
name Bulka Fitnes 1/2
image 31
...
...
...
[PRG] 12
name TOST
...

什麼。我在那做的是每一個名稱分配給它的編號。我知道我可以使用File.ReadAllLines爲每行創建一個字符串表。
我只是不知道如何找到「[PRG]」短語,讀下一個單詞,跳過一個單詞,並閱讀其餘的行。

我在C++的解決方案看起來像這一點,我想在這裏實現

void CFtp::GetNamesList(std::string nameTab[]) 
{ 
    int numberBuff; 
    wstring textBuff; 
    wstring PRG = L"PRG"; 
    wifstream file(m_localPath + "programs.prg", std::ios::binary); 
    if (file.good()) { 
     while (!file.eof()) { 
      file >> textBuff; 
      if (textBuff.find(PRG) != string::npos) { 
       file >> numberBuff; 
       file >> textBuff; 
       getline(file, textBuff); 
       textBuff.erase(0, 1); 
       textBuff.erase(textBuff.length() - 1, 1); 
       nameTab[numberBuff] = convert.to_bytes(textBuff); 
      } 
     } 
    } 
    file.close(); 
} 
+0

如果您期待c#答案,請不要發佈C++代碼。此外,請張貼你已經嘗試在C# –

回答

0

同樣的事情,讓我知道如果這能幫助...

string line; 

// Read the file and display it line by line. 
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt"); 
while((line = file.ReadLine()) != null) 
{ 
    List<string> ListOfParsedValues = line.Trim().Split(' ').ToList(); 
    if (ListOfParsedValues[0] == "[PRG]") 
    { 
     string desiredValue = //ListOfParsedValues.Last() ... or whatever you want here 
    } 
} 

file.Close(); 
+0

它會幫助,但我得到一個錯誤:「不能隱式轉換類型'字符串[]'爲'System.Collections.Generic.List '」。這對於這個文件來說是完美的,因爲我只是從ListOfParsedValues中讀取第二個單詞來獲取名稱的索引。在下一行我可以做同樣的事情,並刪除第一個單詞,我會得到一個名字。 – WhiteBambo

+0

我忘了做.ToList()在拆分 –

0

類似的東西?

static void Read() 
{ 
    string[] myFile = File.ReadAllLines(@"C:\1.txt"); 

    for(int index = 0; index < myFile.Length; index++) 
    { 
     if(myFile[index].Contains("[PRG]")) 
     {    
      Console.WriteLine(myFile[index + 1].Substring(myFile[index + 1].IndexOf(' ')).Trim()); 
     } 
    } 
} 

static void Main(string[] args) 
{ 
    Read(); 
} 

您可以編輯代碼,例如:

  • 傳遞文件路徑讀取功能。
  • instad顯示結果您可以添加值列表,並從函數返回的列表...

更多...

+0

之後,我可以繼續使用它,但是我無法替換「name」這個詞組,因爲在寫入名稱的語言中,其中一些名稱包含「name」作爲字。 – WhiteBambo

+0

在這種情況下,你可以使用myFile [index + 1] .Substring(myFile [index + 1] .IndexOf(''))//我會改變例子 –

-2

寫成一個控制檯應用程序,只是爲了說明什麼你需要,我會做這樣的事情:

using System; 
using System.Collections.Generic; 
using System.IO; 

namespace demo 
{ 
    struct info 
    { 
     public int code; 
     public string infoWanted; 
    } 
    class Program 
    { 
     public static Dictionary<String, Type> animals; 
     static void Main(string[] args) 
     { 
      string [] lines = File.ReadAllLines(@"fullPath"); 
      List<info> myInfo = new List<info>(); 
      for (int i = 0; i < lines.Length; i++) 
      { 
       int targetCode = -1; 
       string infoWanted = ""; 
       string l = lines[i]; 
       int prg = l.IndexOf("[PRG]"); 
       if (prg > -1) 
       { 
        string subLine = l.Substring(prg + 6).Trim(); 
        int whiteSpace = subLine.IndexOf(" "); 
        if (whiteSpace > 0) 
        { 
         subLine = subLine.Substring(0, whiteSpace); 
        } 
        if (!int.TryParse(subLine, out targetCode)) 
        { 
         targetCode = -1; 
        } 
        if (targetCode > -1) 
        { 
         i++; 
         if (i == lines.Length) 
         { 
          break; 
         } 
         l = lines[i].Trim(); 
         whiteSpace = l.IndexOf(" "); 
         if (whiteSpace > 0) 
         { 
          infoWanted = l.Substring(whiteSpace + 1); 
          //do something with target code and infoWanted  
          info newInfo = new info(); 
          newInfo.code = targetCode; 
          newInfo.infoWanted = infoWanted; 
          myInfo.Add(newInfo); 
         } 
        } 
       } 
      } 
      foreach (info i in myInfo) 
      { 
       Console.WriteLine(i.code.ToString() + ": " + i.infoWanted); 
      } 
      Console.ReadKey(); 
     } 
    } 
} 

注意在萊昂的答案中使用替換是危險的;首先它假定要忽略的第一個單詞總是「名稱」(這可能是真的),但其次,更重要的是,替換將刪除該行中的所有事件。最好是保證安全,尋找第一個白色空間,並在此之後採取一切行動。此外,我的解決方案允許「[PRG]」不是該線的第一部分。

相關問題