Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
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.
Posted
Comments
Adam Zgagacz 31-Dec-13 18:21pm    
Do you have any code to share with us? What did you do so far? Where are you stuck?
Member 10304617 31-Dec-13 18:54pm    
I have sample and basic code that read data from text file... I dont know how to divide the data into two arrays
BillWoodruff 31-Dec-13 19:01pm    
Arrays of what Type ? String ? Int32 ?
Member 10304617 31-Dec-13 19:02pm    
uint
Adam Zgagacz 31-Dec-13 20:10pm    
Are arrays, strict requirement? Problem is that when reading from file you will not know size of arrays at the start of reading, and arrays are not meant to be re-sized.
If you switch to lists instead of arrays it would be much easier.

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[^].

[EDIT]

Member 10304617 wrote:
In addition I would like to ask you, how can I check that I read uint and hex number?
This is easy:
C#
uint value;
bool isValid = uint.TryParse(someString, out value);

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
 
Share this answer
 
v3
Comments
Member 10304617 1-Jan-14 12:43pm    
thanks!
Sergey Alexandrovich Kryukov 1-Jan-14 13:41pm    
You are welcome. Will you accept the answer formally (green "Accept" button)?
In all cases, your follow-up questions will be welcome.

Happy New Year!

—SA
Member 10304617 1-Jan-14 14:15pm    
In addition I would like to ask you ,How can I check that I read uint and hex number?
Sergey Alexandrovich Kryukov 1-Jan-14 15:09pm    
Sure. Please see the update to my answer, after [EDIT].

Happy New Year!

—SA
Member 10304617 1-Jan-14 15:12pm    
thanks!!!!
Happy New Year to you too... :)
Assuming you have read all the lines in the file into one big string:
C#
// 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>
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.
 
Share this answer
 
v2
Comments
Member 10304617 1-Jan-14 12:44pm    
thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900