从文本文件中读取数据,然后使用C#将每一行分配到单独的数组中. [英] Reading data from a text file and assigning each row into separate arrays using C#.
问题描述
假设我有一个文本文件,其数据如下所示,我想读取第一行并将元素存储在一个数组中.读取第二行并存储在第二个数组中,依此类推.稍后,我将对数组进行一些操作.您能帮我用C#做到这一点吗? (我是初学者)
输入文本文件:
5,7,3,6,9,8,3,5,7
5,6,8,3,4,5
6,4,3,2,65,8,6,3,3,5,7,4
4,5,6,78,9,4,2,5,6
我还想检查行数.我将找出每一行的平均值以及每一行的其他类似计算.所以我想遍历列表并为每个数组执行.
这是我一直在尝试的代码:
公共 静态 void ReadInputFile(字符串 [] args)
{
List< float [] > 数组= 新 List< float [] int 计数器= 0 ;
字符串行;
// 读取文件
System.IO.StreamReader文件= 新建 System.IO.StreamReader(" );
同时((line = file.ReadLine())!= 空)
{
// 将该行拆分为,分隔符上的字符串数组
字符串 [] splitLine = line.ToString().Split(' // 如果我们的拆分不是null且长度为正值
如果(splitLine!= 空&& splitLine.Length > 0 )
{
// 创建一个与我们的新字符串相同大小的lineArray []
浮动 [] lineArray = 新 浮动 [ splitLine.Length];
int posCounter = 0 ;
foreach (字符串 splitValue in splitLine中)
{
// 遍历拆分中的每个值,然后尝试进行转换
// 将其转换为int并将其推入数组
尝试
{
lineArray [posCounter] = float .Parse(splitValue);
}
捕获 {}
posCounter ++;
}
// 如果我们的lineArray的长度为正,则为我们的
// 数组列表,供以后处理.
如果(lineArray.Length > 0 )
{
arrays.Add(lineArray);
}
}
计数器++;
}
int [] size = 新 int [柜台];
file.Close();
// 暂停屏幕.
Console.ReadLine();
}
// 读取data.txt文本文件. var data = System.IO.File.ReadAllText(" ); // 创建新的float [] 列表 var 数组= 新列表< float [] > (); // 将数据文件内容分成几行 var 行= data.Split(新 [] {' \ r',' \ n' },StringSplitOptions.RemoveEmptyEntries); // 循环所有行 foreach ( var 行 in 行) { // 创建新列表< float>代表该行中所有用逗号分隔的数字 var lineArray = 新列表< float>(); // 按行滑动,并遍历所有数字值 foreach ( var s 在行中. (新 [] {' ,' },StringSplitOptions.RemoveEmptyEntries)) { // 将转换后的数值添加到我们的lineArray lineArray.Add(Convert.ToInt64(s)); } // 将lineArray添加到主数组 arrays.Add(lineArray.ToArray()); // 重复循环,直到没有更多行为止 } var numberOfRows = lines.Count(); var numberOfValues = arrays.Sum(s = > s.Length);我只显示第一行来读取文本并存储在数组中.希望下面的代码可以为您提供帮助.
公共 class 数据 { 公共 字符串 [] arr = 新 字符串 [ 5 ]; 公共 字符串 [] arr1 = 新 字符串 [ 5 ]; 公共 字符串 m,n; 公共 无效 getdata() { System.IO.StreamReader sr = 新 System.IO.StreamReader(" ); 字符 [] x = 空; 字符 [] y = 空; for ( int i = 1 ; i < 10 ; i ++)// 第一行的示例 { x = 新 字符 [ 1 ]; sr.Read(x, 0 ,x.Length); y = 新 字符 [ 1 ]; sr.Read(y, 0 ,y.Length); m = 新 字符串(x); arr [i] = m; n = 新 字符串(y); arr1 [i] = n; } } }
Suppose I have a text file with data like below, I want to read first row and store the elements in one array. Read the second row and store in second array and so on. I will be making some manipulations on the array later on. Can you help me to do this in C#. (I''m a beginner)
Input text file:
5,7,3,6,9,8,3,5,7
5,6,8,3,4,5
6,4,3,2,65,8,6,3,3,5,7,4
4,5,6,78,9,4,2,5,6
I would also like to check the number of rows. I would be finding out the mean of each row and some other similar calculations on each row. So I would like to loop through the list and carry out for each of the arrays.
This is the code I have been trying around with:
public static void ReadInputFile(string[] args)
{
List<float[]> arrays = new List<float[]>();
int counter = 0;
string line;
// Read the file
System.IO.StreamReader file = new System.IO.StreamReader("Data.txt");
while ((line = file.ReadLine()) != null)
{
// split the line into a string array on , separator
string[] splitLine = line.ToString().Split(',');
// if our split isnt null and has a positive length
if (splitLine != null && splitLine.Length > 0)
{
// create a lineArray the same size as our new string[]
float[] lineArray = new float[splitLine.Length];
int posCounter = 0;
foreach (string splitValue in splitLine)
{
// loop through each value in the split, try and convert
// it into an int and push it into the array
try
{
lineArray[posCounter] = float.Parse(splitValue);
}
catch { }
posCounter++;
}
// if our lineArray has a positive length then at it to our
// list of arrays for processing later.
if (lineArray.Length > 0)
{
arrays.Add(lineArray);
}
}
counter++;
}
int[] size = new int[counter];
file.Close();
// Suspend the screen.
Console.ReadLine();
}
// Read the data.txt textfile. var data = System.IO.File.ReadAllText("Data.txt"); // Create a new List of float[] var arrays = new List<float[]>(); // Split data file content into lines var lines = data.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); // Loop all lines foreach (var line in lines) { // Create a new List<float> representing all the commaseparated numbers in this line var lineArray = new List<float>(); // Slipt line by , and loop through all the numeric valus foreach (var s in line.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)) { // Add converted numeric value to our lineArray lineArray.Add(Convert.ToInt64(s)); } // Add lineArray to main array arrays.Add(lineArray.ToArray()); // Loop repeats until there are noe more lines } var numberOfRows = lines.Count(); var numberOfValues = arrays.Sum(s => s.Length);
Hi..I only showed for the first row to read text and store in array.Hope below code can help you.
public class Data { public string[] arr = new string[5]; public string[] arr1 = new string[5]; public string m,n; public void getdata() { System.IO.StreamReader sr = new System.IO.StreamReader("Data.txt"); char[] x = null; char[] y = null; for (int i = 1; i < 10; i++) //example for first row { x = new char[1]; sr.Read(x, 0, x.Length); y = new char[1]; sr.Read(y, 0, y.Length); m = new string(x); arr[i] = m; n = new string(y); arr1[i] = n; } } }
这篇关于从文本文件中读取数据,然后使用C#将每一行分配到单独的数组中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!