C# 从文本文件中读取整数并将其存储在列表中
我有一个小文本文件,在单独的行中包含几个整数 我编写了以下程序(只是一个名为C# 从文本文件中读取整数并将其存储在列表中,c#,C#,我有一个小文本文件,在单独的行中包含几个整数 我编写了以下程序(只是一个名为ReadFromFile)的函数),以便读入整数并将其分配给一些变量 我想知道我是否能改进它,以及如何改进?我尝试读取整数,但意识到使用StreamReader会出错,所以我继续使用字符串 我有没有办法改进这个计划 它所做的就是读入下面的数字,分配前两到两个变量,然后把其余的放在一个列表中 3 4 8 8 8 8 8 8 因此,我将有:var1=3,var2=4,myList=[8,8,8,8,8] using Sys
ReadFromFile
)的函数),以便读入整数并将其分配给一些变量
我想知道我是否能改进它,以及如何改进?我尝试读取整数,但意识到使用StreamReader
会出错,所以我继续使用字符串
我有没有办法改进这个计划
它所做的就是读入下面的数字,分配前两到两个变量,然后把其余的放在一个列表中
3
4
8
8
8
8
8
8
因此,我将有:var1=3
,var2=4
,myList=[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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
名称空间实践
{
班级计划
{
静态void Main(字符串[]参数)
{
//从文件中读取规范。
ReadFromFile();
//防止控制台窗口关闭。
Console.ReadLine();
}
///从文件中读取输入规范的函数。
公共静态void ReadFromFile()
{
字符串localPath=@“C:\Desktop\input.txt”;
StreamReader sr=新的StreamReader(localPath);
//将文件中的所有行读取到列表中,
//其中每个列表元素是一行。
//文件中的每一行。
字符串行=null;
//文件的所有行。
列表行=新列表();
而((line=sr.ReadLine())!=null)
{
行。添加(行);
控制台写入线(行);
}
//显示提取的参数。
Console.WriteLine(第[0]+行“var1”);
Console.WriteLine(第[1]行+变量2”);
//把剩下的放在一个单独的列表中。
List myList=新列表();
对于(int i=2;i
如果有标题行,可能需要跳过(…);例如,要匹配(int i=2;…)的,
:
你可以这样写:
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] );
}
}
publicstaticvoidreadfromfile(stringlocalpath)//对路径进行参数化以获得更大的灵活性
{
StreamReader sr=新的StreamReader(localPath);
//从文件中提取行
List lines=Regex.Split(sr.ReadToEnd(),“\r\n”)。选择(int.Parse.ToList();
//我们可以关闭阅读器,因为我们不再需要它了
高级关闭();
Console.WriteLine(第[0]+行“var1”);
Console.WriteLine(第[1]行+变量2”);
//删除前2个元素
lines=lines.Skip(2.ToList();
对于(int i=0;i
可选:如果文件在一行上有一个从非数字到大的数字,则使用int.TryParse代替int.Parse以确保安全。@BrilBroeder同意,但a:我试图与OP的代码保持一致(使用int.Parse
),b:从LINQ:)使用这有点尴尬@MarcGravel-OP确实有一个var1=3
,var2=4
他的问题中也有要求。谢谢大家。谢谢你,雷米。这也提醒我应该开始学习正则表达式。这里使用正则表达式拆分是因为String.split()方法只能拆分1个字符,这里我们需要拆分2个字符
var vals = File.ReadAllLines(path).Skip(2).Select(int.Parse).ToList();
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] );
}
}