解释以C#文本形式保存的数据 [英] Interpriting data held in text in c#

查看:57
本文介绍了解释以C#文本形式保存的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须阅读一个文本文件并将信息加载到我的程序中

艾米·黄/格拉斯哥
简·福斯特/纽卡斯尔
1,3,2
2,1,2
3,4,9

到目前为止,我有

StreamReader LiveData =新的StreamReader(Convert.ToString(openFileDialog1.FileName));
label2.Text = LiveData.ReadLine();
label4.Text = LiveData.ReadLine();

这会将名称放在正确的位置,哈哈.

我需要将数据加载到程序的可用字符串中.

前两个是玩家名称.下面的线代表round,player1,player2.

我需要
字符串播放器1
字符串播放器2
圆线
字符串score1
字符串score2

这让我头疼,我可以读取数据,但不知道如何过滤.我的程序非常简单,它将数据显示给用户.其目的是在投影仪上显示游戏结果.

我的原始程序允许手动输入分数,但是如果我每隔几秒钟即可自动读取此文件,那就更好了.我已经进行了一段时间编程,但是我很难理解文本信息.

I have to read a text file and load the info into my program

Amy wong / Glasgow
Jane foster / Newcastle
1,3,2
2,1,2
3,4,9

So far I have

StreamReader LiveData = new StreamReader(Convert.ToString(openFileDialog1.FileName));
label2.Text = LiveData.ReadLine();
label4.Text = LiveData.ReadLine();

This gets the names in the right place LOL.

I need to load the data into usable strings for my program.

the first two are the player names. The lines underneath represent round,player1,player2.

I need
string player1
string player2
string round
string score1
string score2

this is just giving me a headache I can read the data but have no idea how I can filter it. My program is quite simple it displays the data to the user. Its purpose is for displaying results of games on a projector.

My original program allowed manual entry of scores but if I could automaticly read this file every few seconds it would be much better. I have been programming for a while now but I find interpreting text information hard.

推荐答案

尝试类似这样的方法:

Try something like this:

StreamReader LiveData = new StreamReader(Convert.ToString(openFileDialog1.FileName));
string[] data = LiveData.ReadLine().Split(',');//Reads a line and splits it at the delimiter (',' in this case) into an array of strings.

//You can then process the strings in the array

LiveData.Close();


以下代码可用于将文件中的所有行读取到string array中,然后将播放器存储在数组中,该回合和得分信息存储在字典中,以便使用该键轻松检索,以便以后在程序中使用.该解决方案适用于前两行中的玩家,然后重复进行回合和得分数据,因为得分数据是循环读取的.
The following code can be used to read all the lines from the file into a string array, then the players are stored in an array and the round and score info is stored in a dictionary for easy retrieval using the key for later use in the program. The solution works for players in the first two lines and then repeating round and score data, as the score data is read in a loop.
void Main()
{
    string[] scoresFromFile = System.IO.File.ReadAllLines(openFileDialog1.FileName);
    string[] players = {scoresFromFile[0],scoresFromFile[1]};
    Dictionary<int,Score> scores = new Dictionary<int,Score>();
    string[] tempScores;
    for(int i=2; i<scoresFromFile.Length; i++){
        tempScores = scoresFromFile[i].Split(',');
        scores.Add(Convert.ToInt32(tempScores[0]), new Score(Convert.ToInt32(tempScores[1]),Convert.ToInt32(tempScores[2])));
    }
    Console.WriteLine ("Players:");
    Console.WriteLine ("--{0}\n--{1}",players[0],players[1]);
    foreach (int key in scores.Keys)
    {
        Console.WriteLine ("Round: {0}",key);
        Console.WriteLine ("---Score1:{0}\n---Score2:{1}",scores[key].Score1,scores[key].Score2);
    }
}
public struct Score {
    public int Score1;
    public int Score2;
    
    public Score (int score1, int score2){
    	Score1=score1;
    	Score2=score2;
    }
}
//Output from the above code:
//Players:
//--Amy wong / Glasgow
//--Jane foster / Newcastle
//Round: 1
//---Score1:3
//---Score2:2
//Round: 2
//---Score1:1
//---Score2:2
//Round: 3
//---Score1:4
//---Score2:9


LINQPad,可从此处下载 http://www.linqpad.net/ [


LINQPad, which can be downloaded from here http://www.linqpad.net/[^], can be used for quick test of the above code.


编写更多内容识别球员的名字,回合和得分.那么您将轻松地从文本文件中读取文本.
write something more to identify players name, round and score. then you will easily read text from the text file.


这篇关于解释以C#文本形式保存的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆