0

Ok I've got a txt file named "info.txt" which includes the following text:

[entry]
title = Hello World
info = sometext
number = 0
available = -1

[entry]
title = All Vids
info = somemoretext
number = 1
available = 0

[entry]
title = All pics
info = somedifferenttext
number = 2
available = -1

[entry]
title = all music
info = differenttext
number = 3
available = 0

On C# What I want to do is open this file and search for "title = " and then get any words after it and then put it inside a text box. So for example, after it looks for "title = " I want it to put "Hello World" inside textbox1. Then if there is another "title = " which would be "All Vids" I want to put it inside textbox2. The same should be done if there are more instances of "title = " which should be placed into textbox3, textbox4 and so on.

This is what I worked on which I found from another answer:

        private void button1_Click(object sender, EventArgs e)
    {
        List<List<string>> groups = new List<List<string>>();
        List<string> current = null;
        foreach (var line in File.ReadAllLines(@"C:\Users\Rohul\Documents\info.txt"))
        {
            if (line.Contains("title") && current == null)
                current = new List<string>();
            else if (line.Contains("info") && current != null)
            {
                groups.Add(current);
                current = null;
            }
            if (current != null)
            richTextBox1.Text = line;
        }
    }

The problem with this it reads the full line and the last entry is read

I hope someone can help me. Thanks in advance

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 2
    You have explained very well your _requirements_ what you have not done is to show any effort to solve the problem. Do you just wait for someone to write the code for you? Please read the [help] and in particular [ask] – Steve Jul 09 '16 at 14:05
  • Use a `For-Loop` to iterate over all lines in the text file, then use an `IF` statement to check for lines starting with *title =*. This is elementary, my nephew in primary school could code this. Please google the terms I've supplied and try and work it out yourself. Welcome to [so], it is a website for professional programmers, NOT a do my home work for me site. Good luck! – Jeremy Thompson Jul 09 '16 at 14:09
  • Thanks for the help. I've added what I've worked on so far. I'm trying to self learn how to code so I'm kinda new –  Jul 09 '16 at 14:18
  • @RohulAhmed Would you be able to improve the grammar (beyond the code) some? It's a little hard to read in places. – Claudia Jul 09 '16 at 14:19
  • @RohulAhmed check the answer below, it should work for you. – Zein Makki Jul 09 '16 at 14:19

2 Answers2

2

Consider your data is in a file named data.txt.

Logic: Read the data, split by new line, find lines containing "title =". Remove that identifier and take the rest of the line.

string data = File.ReadAllText("data.txt");
string identifier = "title =";

List<string> results = 
    data.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
    .Where(x => x.Contains(identifier))
    .Select(x => x.Replace(identifier, String.Empty).Trim()).ToList();

After that, you'll have list of strings in results. Do whatever you want with it.

If you need to read it line by line like you tried, then:

string identifier = "title =";
string data = File.ReadAllText("data.txt");

List<String> results = new List<string>();

foreach(string line in File.ReadAllLines("data.txt"))
{
    if(line.Contains(identifier))
    {
        results.Add(line.Replace(identifier, string.Empty).Trim());
    }
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

It sounds like you are trying to read an INI FILE. If this is your purpose take a look at this article An INI file handling class using C# or Reading/writing an INI file from StackOverflow.

Community
  • 1
  • 1
Carlos Caldas
  • 806
  • 11
  • 12
  • Thanks for the help. Yep your kinda right. But the problem is, if you look at my text file it has multiple instances of [Entry] and I can only read the first [Entry] using your method. I don't know how to read the other 3 [Entry] –  Jul 10 '16 at 13:55