C# 如何将.txt文件中的数字读取为整数数组?
我有一个文件需要保存为数组。我正在尝试使用C# 如何将.txt文件中的数字读取为整数数组?,c#,arrays,streamreader,C#,Arrays,Streamreader,我有一个文件需要保存为数组。我正在尝试使用StreamReader将文本文件转换为整数数组。我只是不确定在代码末尾的for循环中应该放什么 这就是我到目前为止所做的: //Global Variables int[] Original; //Load File private void mnuLoad_Click_1(object sender, EventArgs e) { //code to load the numbers from a file OpenFileDialo
StreamReader
将文本文件转换为整数数组。我只是不确定在代码末尾的for循环中应该放什么
这就是我到目前为止所做的:
//Global Variables
int[] Original;
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
OpenFileDialog fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
//open file to read
StreamReader sr = new StreamReader(fd.OpenFile());
int Records = int.Parse(sr.ReadLine());
//Assign Array Sizes
Original = new int[Records];
int[] OriginalArray;
for (int i = 0; i < Records; i++)
{
//add code here
}
}
PS我是一个初学者,所以我的编码技能非常基础
更新:我以前有使用Line.Split
然后将文件映射到数组的经验,但显然这不适用于这里,所以我现在该怎么办
//as continued for above code
for (int i = 0; i < Records; i++)
{
int Line = int.Parse(sr.ReadLine());
OriginalArray = int.Parse(Line.Split(';')); //get error here
Original[i] = OriginalArray[0];
}
//以上代码继续
for(int i=0;i
您应该能够使用与上面类似的代码:
OriginalArray[i] = Convert.ToInt32(sr.ReadLine());
每次调用
sr.ReadLine
时,它都会将数据指针增加1行,从而遍历文本文件中的数组。您应该能够使用与上面的代码类似的代码:
OriginalArray[i] = Convert.ToInt32(sr.ReadLine());
每次调用
sr.ReadLine
时,它都会将数据指针增加1行,从而遍历文本文件中的数组。您应该能够使用与上面的代码类似的代码:
OriginalArray[i] = Convert.ToInt32(sr.ReadLine());
每次调用
sr.ReadLine
时,它都会将数据指针增加1行,从而遍历文本文件中的数组。您应该能够使用与上面的代码类似的代码:
OriginalArray[i] = Convert.ToInt32(sr.ReadLine());
每次调用sr.ReadLine
时,它都会将数据指针增加1行,从而遍历文本文件中的数组。试试这个
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(fd.OpenFile());
var list = new List<int>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
int value = 0;
if (!string.IsNullOrWhiteSpace(line) && int.TryParse(line, out value))
list.Add(value);
}
MessageBox.Show(list.Aggregate("", (x, y) => (string.IsNullOrWhiteSpace(x) ? "" : x + ", ") + y.ToString()));
}
OpenFileDialog fd=newopenfiledialog();
如果(fd.ShowDialog()==DialogResult.OK)
{
StreamReader=新的StreamReader(fd.OpenFile());
var list=新列表();
而(!reader.EndOfStream)
{
var line=reader.ReadLine();
int值=0;
if(!string.IsNullOrWhiteSpace(line)和&int.TryParse(line,out value))
列表。添加(值);
}
MessageBox.Show(list.Aggregate(“,(x,y)=>(string.IsNullOrWhiteSpace(x)?”:x+“,”)+y.ToString());
}
试试这个
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(fd.OpenFile());
var list = new List<int>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
int value = 0;
if (!string.IsNullOrWhiteSpace(line) && int.TryParse(line, out value))
list.Add(value);
}
MessageBox.Show(list.Aggregate("", (x, y) => (string.IsNullOrWhiteSpace(x) ? "" : x + ", ") + y.ToString()));
}
OpenFileDialog fd=newopenfiledialog();
如果(fd.ShowDialog()==DialogResult.OK)
{
StreamReader=新的StreamReader(fd.OpenFile());
var list=新列表();
而(!reader.EndOfStream)
{
var line=reader.ReadLine();
int值=0;
if(!string.IsNullOrWhiteSpace(line)和&int.TryParse(line,out value))
列表。添加(值);
}
MessageBox.Show(list.Aggregate(“,(x,y)=>(string.IsNullOrWhiteSpace(x)?”:x+“,”)+y.ToString());
}
试试这个
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(fd.OpenFile());
var list = new List<int>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
int value = 0;
if (!string.IsNullOrWhiteSpace(line) && int.TryParse(line, out value))
list.Add(value);
}
MessageBox.Show(list.Aggregate("", (x, y) => (string.IsNullOrWhiteSpace(x) ? "" : x + ", ") + y.ToString()));
}
OpenFileDialog fd=newopenfiledialog();
如果(fd.ShowDialog()==DialogResult.OK)
{
StreamReader=新的StreamReader(fd.OpenFile());
var list=新列表();
而(!reader.EndOfStream)
{
var line=reader.ReadLine();
int值=0;
if(!string.IsNullOrWhiteSpace(line)和&int.TryParse(line,out value))
列表。添加(值);
}
MessageBox.Show(list.Aggregate(“,(x,y)=>(string.IsNullOrWhiteSpace(x)?”:x+“,”)+y.ToString());
}
试试这个
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(fd.OpenFile());
var list = new List<int>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
int value = 0;
if (!string.IsNullOrWhiteSpace(line) && int.TryParse(line, out value))
list.Add(value);
}
MessageBox.Show(list.Aggregate("", (x, y) => (string.IsNullOrWhiteSpace(x) ? "" : x + ", ") + y.ToString()));
}
OpenFileDialog fd=newopenfiledialog();
如果(fd.ShowDialog()==DialogResult.OK)
{
StreamReader=新的StreamReader(fd.OpenFile());
var list=新列表();
而(!reader.EndOfStream)
{
var line=reader.ReadLine();
int值=0;
if(!string.IsNullOrWhiteSpace(line)和&int.TryParse(line,out value))
列表。添加(值);
}
MessageBox.Show(list.Aggregate(“,(x,y)=>(string.IsNullOrWhiteSpace(x)?”:x+“,”)+y.ToString());
}
您可以将整个文件读入字符串数组,然后进行解析(检查每个文件的完整性)
比如:
int[] Original;
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
var fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
var file = fd.FileName;
try
{
var ints = new List<int>();
var data = File.ReadAllLines(file);
foreach (var datum in data)
{
int value;
if (Int32.TryParse(datum, out value))
{
ints.Add(value);
}
}
Original = ints.ToArray();
}
catch (IOException)
{
// blah, error
}
}
}
int[]原件;
//加载文件
私有void mnuLoad_Click_1(对象发送方,事件参数e)
{
//从文件中加载数字的代码
var fd=新建OpenFileDialog();
//打开“文件”对话框并检查是否选择了文件
如果(fd.ShowDialog()==DialogResult.OK)
{
var file=fd.FileName;
尝试
{
var ints=新列表();
var data=File.ReadAllLines(文件);
foreach(数据中的var数据)
{
int值;
if(Int32.TryParse(基准,输出值))
{
积分(增值);
}
}
原始=ints.ToArray();
}
捕获(IOException)
{
//诸如此类的错误
}
}
}
您可以将整个文件读入字符串数组,然后进行解析(检查每个文件的完整性)
比如:
int[] Original;
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
var fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
var file = fd.FileName;
try
{
var ints = new List<int>();
var data = File.ReadAllLines(file);
foreach (var datum in data)
{
int value;
if (Int32.TryParse(datum, out value))
{
ints.Add(value);
}
}
Original = ints.ToArray();
}
catch (IOException)
{
// blah, error
}
}
}
int[]原件;
//加载文件
私有void mnuLoad_Click_1(对象发送方,事件参数e)
{
//从文件中加载数字的代码
var fd=新建OpenFileDialog();
//打开“文件”对话框并检查是否选择了文件
如果(fd.ShowDialog()==DialogResult.OK)
{
var file=fd.FileName;
尝试
{
var ints=新列表();
var data=File.ReadAllLines(文件);
foreach(数据中的var数据)
{
int值;
if(Int32.TryParse(基准,输出值))
{
积分(增值);
}
}
原始=ints.ToArray();
}
捕获(IOException)
{
//诸如此类的错误
}
}
}
您可以将整个文件读入字符串数组,然后进行解析(检查每个文件的完整性)
比如:
int[] Original;
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
var fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
var file = fd.FileName;
try
{
var ints = new List<int>();
var data = File.ReadAllLines(file);
foreach (var datum in data)
{
int value;
if (Int32.TryParse(datum, out value))
{
ints.Add(value);
}
}
Original = ints.ToArray();
}
catch (IOException)
{
// blah, error
}
}
}
int[]原件;
//加载文件
私有void mnuLoad_Click_1(对象发送方,事件参数e)
{
//从文件中加载数字的代码
var fd=新建OpenFileDialog();
//打开“文件”对话框并检查是否选择了文件
if(fd.ShowDialog()=