Click here to Skip to main content
16,021,172 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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();
        }
Posted
Comments
Karthik Harve 18-Jul-12 5:32am    
what is a problem you are facing? Are you getting any error?
CrouchingViper 18-Jul-12 5:45am    
I am not able to get the number or rows as arrays. Also I'm struggling to find the size of the array without first parsing all the elements.

C#
// 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);
 
Share this answer
 
v4
Comments
CrouchingViper 18-Jul-12 5:52am    
Here you are converting to Int64 type. Is there a conversion for float?
CrouchingViper 18-Jul-12 6:05am    
If I want the average of say, Rows 1-3 stored in another array, How am I to do it?
StianSandberg 18-Jul-12 6:10am    
To get the average of an array of floats:
var av = arrFloats.Average();
CrouchingViper 18-Jul-12 6:23am    
I'm sorry if I am being dumb here, but, if I want to say append average of row1 to av[1] and average of row2 to av[2], how would I go about doing that?

When I ran the above code, the numberOfRows and numberOfValues were wrong.
StianSandberg 18-Jul-12 6:35am    
added one more StringSplitOptions.RemoveEmptyEntries in first split to remove empty lines. Now the number of lines is correct. numberOfValues is correct when i run it. numberOfValues gives you the total number of values in the whole file.
Hi..I only showed for the first row to read text and store in array.Hope below code can help you.


C#
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;
   }
 }
}
 
Share this answer
 

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