[英]Reading in integers from text files and storing them in lists
I have a small text file, containing a few integers on separate lines. 我有一个小的文本文件,在单独的行中包含一些整数。
I wrote the following program (just a function called ReadFromFile
) in order to read in the integers and assign them to some variables. 我编写了以下程序(只是一个名为
ReadFromFile
的函数),以便读取整数并将它们分配给某些变量。
I wonder if I could improve it, and how? 我想知道是否可以改善它,如何改善 ? I tried reading in integers, but realized I would get errors with
StreamReader
, so I carried on using strings. 我尝试读取整数,但意识到使用
StreamReader
会出错,所以我继续使用字符串。
Is there anyway I could improve this program? 无论如何,我可以改进此程序吗?
All it does is read in the following numbers, assign the first two to two variables, and put the rest in a list. 它所做的全部工作都读入以下数字,将前两个变量分配给两个变量,然后将其余变量放在列表中。
3
4
8
8
8
8
8
8
So, I will have: var1 = 3
, var2 = 4
, myList = [8,8,8,8,8,8]
因此,我将拥有:
var1 = 3
, var2 = 4
, myList = [8,8,8,8,8,8]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Practice
{
class Program
{
static void Main(string[] args)
{
// Read the specifications from the file.
ReadFromFile();
// Prevent the console window from closing.
Console.ReadLine();
}
/// The function that reads the input specifications from a file.
public static void ReadFromFile()
{
string localPath = @"C:\Desktop\input.txt";
StreamReader sr = new StreamReader(localPath);
// Read all the lines from the file into a list,
// where each list element is one line.
// Each line in the file.
string line = null;
// All lines of the file.
List<string> lines = new List<string>();
while ( ( line = sr.ReadLine() ) != null )
{
lines.Add(line);
Console.WriteLine(line);
}
// Display the extracted parameters.
Console.WriteLine( lines[0] + " var1");
Console.WriteLine( lines[1] + " var2");
// Put the rest in a separate list.
List<int> myList = new List<int>();
for (int i = 2; i < lines.Count; i++)
{
Console.WriteLine("item {0} = {1}", i-1, lines[i] );
myList.Add( Int32.Parse( lines[i] ) );
}
sr.Close();
}
}
}
var vals = File.ReadAllLines(path).Select(int.Parse).ToList();
You might need a Skip(...)
if you have header lines; 如果您有标题行,则可能需要
Skip(...)
; for example to match your for(int i = 2; ...)
: 例如匹配您的
for(int i = 2; ...)
:
var vals = File.ReadAllLines(path).Skip(2).Select(int.Parse).ToList();
You could write it as follow : 你可以这样写:
public static void ReadFromFile(string localPath) // paremetrizing the path for more flexibility
{
StreamReader sr = new StreamReader(localPath);
// extrating the lines from the file
List<int> lines = Regex.Split(sr.ReadToEnd(), "\r\n").Select(int.Parse).ToList();
// we can close the reader as we don't need it anymore
sr.Close();
Console.WriteLine( lines[0] + " var1");
Console.WriteLine( lines[1] + " var2");
// removing the first 2 elements
lines = lines.Skip(2).ToList();
for (int i = 0; i < lines.Count; i++)
{
Console.WriteLine("item {0} = {1}", i-1, lines[i] );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.