在文本文件中搜索一个短语并在C中读取下一个单词#

问题描述:

我是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# –

同样的事情,让我知道如果这能帮助...

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()在拆分 –

类似的东西?

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(''))//我会改变例子 –

写成一个控制台应用程序,只是为了说明什么你需要,我会做这样的事情:

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]”不是该线的第一部分。