如何从文本文件读取整数到数组 [英] How to read integers from a text file to array

查看:84
本文介绍了如何从文本文件读取整数到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想做的.我对此很满意,但我希望你能忍受我.对我来说这是一个非常新的概念.

So this is what I would like to do. I am kind of all over the place with this but I hope you can bear with me. This is a very new concept to me.

1)在我的程序中,我希望创建一个由50个整数组成的数组,以保存来自文件的数据.我的程序必须获取用户的Documents文件夹的路径.2)文件名将为"grades.txt".直接在您的程序中编码此文件名.无需用户输入即可获取文件名.3)使用此路径创建一个StreamReader对象.这将打开文件.编写一个循环,从文件中读取数据,直到发现文件结尾为止.4)当读取每个整数值时,我将其显示出来并将其存储在数组中.5)使用部分填充数组的概念,编写一个将数组作为参数并计算并返回存储在数组中的整数的平均值的方法输出平均值.

1) In my program I wish create an array of 50 integers to hold the data that comes from the file. My program must get the path to the user's Documents folder. 2) The name of the file will be "grades.txt". Code this file name right in your program. No user input is required to get the file name. 3) Create a StreamReader object, using this path. This will open the file. Write a loop that reads data from the file, until it discovers the end of the file. 4) As each integer value is read in, I display it, and store it in the array. 5) Using the concepts of partially filled arrays, write a method that takes the array as a parameter and calculates and returns the average value of the integers stored in the array Output the average.

因此,现在我很难解决如何获取保存在grades.txt文件中的数字,将其保存到数组中并显示它们的问题.我尝试拆分整数并将其保存为该整数,但这似乎不起作用.

So right now I am having a very hard time figuring out how to get the numbers saved in the grades.txt file, save them to an array, and display them. I try to split the integers and save them as that but it doesn't seem to work.

这是我到目前为止的代码:

This is the code that I have so far:

class Program
{
    const int SIZE = 50;

    static void Main()
    {

        // This line of code gets the path to the My Documents Folder

        int zero = 0;
        int counter = 0;
        int n, m;
        StreamReader myFile;
        myFile = new StreamReader("C:/grades.txt");


        string inputNum = myFile.ReadLine();

        do
        {
            Console.Write("The test scores are listed as follows:");
            string[] splitNum = myFile.Split();
            n = int.Parse(splitNum[0]);
            {
                if (n != zero)
                {
                    Console.Write("{0}", n);                      
                    counter++;
                }
            }
        } while (counter < SIZE && inputNum != null);

        // now we can use the full path to get the document




        Console.ReadLine();
    }
}

这是grades.Txt文件:
88
90
78
65
50
83
75
23
60
94

This is the grades.Txt file:
88
90
78
65
50
83
75
23
60
94

推荐答案

要读取文件,您需要以下内容:

For reading the file you need something like this:

var scores = new List<int>();
        StreamReader reader = new StreamReader("C:/grades.txt");
        while (!reader.EndOfStream)
        {
            int score;
            if (int.TryParse(reader.ReadLine(), out score) && score != 0)
                scores.Add(score);
        }

您可以使用scores.Count属性获得分数计数.

and you can have count of scores with scores.Count property.

这篇关于如何从文本文件读取整数到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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