简体   繁体   中英

How can I read a specific part of a text file?

In C, I was able to read a specific part of a text file like this:

game_results new_statistic(FILE* input, int* rounds) {
    game_results out;

   char temp[300];
   if(fgets(temp, 300, input) != NULL) {
      if(strlen(temp) < 3) {
         fgets(temp, 300, input);
         ++*rounds;
      }

   /* Sorting the string and giving all the variables to the struct game_results */
   sscanf(temp, "%s %d / %d %s %s - %s %d - %d %lf" , out.weekday, &out.date_day, &out.date_month, out.timet,
   out.hometeam, out.awayteam, &out.home_goal, &out.away_goal, &out.crowd);
   out.rounds = *rounds;
   }
   return out;
}

How is it possible to do it like that in C#?

I need specific information that is in the text file. I want to read the value of Amount of Rooms, then the program needs to know that the first roomname is "Stue" and has the value 1 which represents powerconnectors in the room. Next it will read "tv", then 200 which is the powerusage, and 3 for standbyusage. Then it will do the same for the room kitchen.

The text in the file looks the following:

Amount of Rooms: 2
Stue, 1
tv, 200, 3
Kitchen, 1
Fridge, 100, 2

You can use the File.ReadLines method to open a file and query it for the data you need, and it'll only read enough lines to satisfy your query.

So to get the first line, use LINQ's First() method, then split on the colon and grab the number.

var rooms = Convert.ToInt32(File.ReadLines(@"c:\yourFile.txt").First().Split(':').Last());

To get the rest, you could do it a few different ways. I prefer LINQ, so here's another example.

Read everything except the first line (skip it), then split on the comma and create a dictionary.

var data = (from f in File.ReadLines(@"c:\yourFile.txt").Skip(1)
            let parts = f.Split(',')
            select new { key = parts[0], value = Convert.ToInt32(parts[1]) })
          .ToDictionary(x => x.key, x => x.value);

if the information is on the same lines each time a simple way to do it would be with the Streamreader

        System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Public\TestFolder\EXPORT.TXT");
        while (file.Peek() != -1)
        {
            line = file.ReadLine();
            string test1;
            string readdate;
            test1 = line.Substring(0, 8);//1
            FullName = line.Substring(8, 20);//2
            Address = line.Substring(28, 20);//3
            ModuleNumber = line.Substring(48, 10);//4
            test1 = line.Substring(58, 4);//5
            ReadType = line.Substring(62, 1);//6
            Service = line.Substring(63, 1);//7
            test1 = line.Substring(64, 1);//8
            test1 = line.Substring(65, 2);//9
            test1 = line.Substring(67, 9);//10
       }

This particular code is what I use to read a fixed width file, but you could add each line to a list, I think it would be better if you text file was in a format of either fixed width or delimited.

EDIT: I would have loved to have been able to suggest this in a comment but since I'm not allowed to comment, I put this as an answer with a little more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM