我想随机读取文本文件中的文本行.帮我 ! [英] I want random read a text-line in text file. Help me !

查看:83
本文介绍了我想随机读取文本文件中的文本行.帮我 !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的英语说得很不好.
任何人都可以帮助我解决问题.我想随机读取文本文件中的文本行.救救我吧!
这是我程序的代码(c Sharp,Winform),如何修复它:sigh:

I speak English very bad.
Anyone can help me about a problem. I want random read a text-line in text file. Help me!
This is code of my program (c sharp, winform), how to fix it :sigh:

static void ReadQuestion()
{
    StreamReader reader = File.OpenText("..\\..\\Resources\\taynguyen.txt");
    string input = null;
    while ((input = reader.ReadLine()) != null)
    {
        string str = reader.ReadLine();
        string[] s = str.Split(',');
        Cauhoi = s[0];
        Ans1 = s[1];
        Ans2 = s[2];
        Ans3 = s[3];
        Ans4 = s[4];
        AnsEnd = int.Parse(s[5]);
    }
    reader.Close();
}


首先,感谢您的回答!


First, thanks for answer!

推荐答案

您已经关闭.

如果您不太担心性能(或文件不是太大),只需将所有行读入数组,选择一个随机数(最大数为数组长度的大小)并将其用作数组中的索引.

这可以帮助您摆脱过去必须知道多少行的麻烦.

干杯.
You''re close.

If you''re not terribly concerned about performance (or the file isn''t too big) just read all lines into an array, pick a random number (the maximum number being the size of the array length) and use that as the index in the array.

This helps you get past having to know how many lines are in.

Cheers.


我明白您的意思,您能帮我修复代码吗?我的代码太糟糕了,以您为例,我觉得这是可以理解的.谢谢(我不会说英语,对不起:sigh:)
I understand the idea you were saying, Can you help me fix code ? I''m coding so bad, a example form you, probably, i things it''s understandable. Thanks (I can''t speak English, sorry :sigh: )


这篇关于我想随机读取文本文件中的文本行.帮我 !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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