从文件中读取并将值插入数组 [英] read from file and insert values to arrays

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

问题描述



我想从文本文件中读取值并将它们插入到2个数组中,第一列到一个数组,第二列到另一个数组。

例如:

文本文件:0x344 0x44353

0x223 0x2342342

0x21323 0x983298

0x908938 0x8382AA



arr1:0x344,0x223,0x21323,0x908938

arr2:0x44353,0x2342342,0x983298,0x8382AA



我该怎么办?



提前谢谢。

Hi,
I want to read values from text file and insert them to 2 arrays, the first column to one array and the second column to another array.
for example :
text file: 0x344 0x44353
0x223 0x2342342
0x21323 0x983298
0x908938 0x8382AA

arr1 : 0x344, 0x223, 0x21323 , 0x908938
arr2 : 0x44353 , 0x2342342,0x983298,0x8382AA

How can I do it?

thank in advance.

推荐答案

分开一个换行,调用 string.Split(new char [] {''})。它将返回由列拆分的字符串数组。使用 uint.Parse(string)将索引0和1的数组解析为 uint



创建两个类型 System.Collections.Generic.List< uint> 的列表。 (你可以创建一个这样的对象数组,所以上面提到的split数组将具有相同的索引,0和1(或更多,所以你可以有任意数量的列)。放入解析 uint 这些列表的值。



最后,当读取文件时,你知道所需的数组长度(通过属性列出列表);使用此列表方法获取数组: http://msdn.microsoft.com/en-us/library/x303t819%28v=vs.110%29.aspx [ ^ ]。







To split a line into column, call string.Split(new char[] {' '}). It will return you the array of strings split by your columns. Parse this array at index 0 and 1 to uint using uint.Parse(string).

Create two lists of the type System.Collections.Generic.List<uint>. (You can create an array of such objects, so the split array mentioned above would have the same indices, 0 and 1 (or more, so you could have any number of columns). Put parsed uint values to those lists.

At the end, when the file are read, you know the required lengths of arrays (through property Count of the lists); obtain arrays using this list method: http://msdn.microsoft.com/en-us/library/x303t819%28v=vs.110%29.aspx[^].



会员10304617写道:
Member 10304617 wrote:

In另外我想问你,我怎么检查我读取uint和十六进制数?

In addition I would like to ask you, how can I check that I read uint and hex number?

这很简单:

This is easy:

uint value;
bool isValid = uint.TryParse(someString, out value);



如果你字符串 someString 无法识别为有效的 uint 值,该调用将返回false并且 value 仍未分配,否则你得到一个有效的值。



现在关于十六进制,十进制或类似的东西。整数值不能是十六进制,十进制或具有任何特定的基本系统,只有表示整数值的字符串。整数值都是二进制,请参阅: http://en.wikipedia.org/wiki/Two%27s_complement [ ^ ](对于无符号值,这是更简单)。



你根本不需要从十六进制中告诉十进制。只需调用上面提到的方法,无论它如何表示为您输入的字符串,都可以获得正确的值。



-SA


If you string someString cannot be recognized as a valid uint value, the call will return false and value remains unassigned, otherwise you get a valid value.

Now about hex, decimal, or anything like that. The integer values cannot be hexadecimal, decimal, or have any particular base system, only strings representing integer values can. The integer values are all "binary", please see: http://en.wikipedia.org/wiki/Two%27s_complement[^] (and for unsigned values this is way simpler).

You simply don't need to tell decimal from hexadecimal. Just call the method mentioned above, to get the correct value no matter how it was represented as string you have on input.

—SA


假设您已将文件中的所有行读入一个大字符串:
Assuming you have read all the lines in the file into one big string:
// use spaces and newlines to demarcate elements
private char[] splitChars = new char[] {' ', '\r', '\n'};

// arrays to store data columns converted to Type UInt32
private UInt32[] col1;
private UInt32[] col2;

// method to create the two Arrays of Type UInt32
private void TranformToArraysOfUInt32(string rawFileData)
{
    List<string> splitList = rawFileData.Split(splitChars, StringSplitOptions.RemoveEmptyEntries).ToList();
    
    col1 = splitList
        .Where
        (
            (str, index) => (index + 1) %2 == 1
        )
        .Select(n => Convert.ToUInt32(n, 16)).ToArray();
    
    col2 = splitList
        .Where
        (
            (str, index) => (index + 1) %2 == 0
        )
        .Select(n => Convert.ToUInt32(n, 16)).ToArray();
    
    // for testing only ...
    foreach (UInt32 theUInt in col1)
    {
        Console.WriteLine("col 1: 0x" + theUInt.ToString("X"));
    }
    
    // for testing only ...
    foreach (UInt32 theUInt in col2)
    {
        Console.WriteLine("col 2: 0x" + theUInt.ToString("X"));
    }
}</string>

此代码将原始文件数据(类型字符串)拆分为类型字符串的通用列表。



然后使用Linq:



1. Where子句使用Linq的'index属性和modulo来选择每个奇数编号或偶数编号的元素列表。



2. Select子句处理将UInt的十六进制字符串表示转换为Type UInt32的任务,创建所有它们的List,转换到一个数组。



请注意,这里没有验证元素被扫描:如果任何元素不是UInt32的有效表示,这是一个错误!



经验表明应该进行某种形式的验证:至少使用try / catch块。



虽然你可以使用UInt32.Tr在Linq代码中yParse以排除格式不正确的元素,可能存在结构问题:假设排除了一个项目:现在你的两个列没有相同数量的元素,我猜会在你的代码中产生问题。



如果格式错误,在列中的同一索引处排除这两个元素的任务可能需要处理不使用Linq。或者,可能会对数据进行预处理,以排除任何可能的一对条目,其中一个或两个条目格式不正确。

This code splits the raw file data (Type string) into a generic List of Type string.

Then Linq is used:

1. the Where clause uses Linq's 'index property, and modulo, to select either every odd-numbered, or even-numbered, element in the List.

2. the Select clause handles the task of converting the hexadecimal string representations of a UInt to Type UInt32, creating a List of all them, which is converted into an Array.

Note that there is no validation of elements being scanned here: if any element is not a valid representation of a UInt32, that's an error !

Experience suggests that some form of validation should take place: at the least the use of a try/catch block.

While you could use UInt32.TryParse inside the Linq code to exclude elements that are not in the correct format, there is a possible "structural" problem: Suppose one item is excluded: now your two "columns" do not have the same number of elements, which I would guess would create problems in your code.

The task of excluding both elements at the same index in the Columns if one is in the wrong format would probably require processing that did not use Linq. Or, possibly, making a preprocessing pass over the data just to exclude any possible pair of entries where one or both was incorrectly formatted.


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

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