Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I'm trying to read a text file which contains numbers separated by a comma. I need to convert it in to a int array and then display the values of array

The contents of the text file: 1,2,2,1,3

how should i do it?
Posted
Updated 5-Sep-17 15:17pm

Hello,

Please Try this code..
In .aspx page
ASP.NET
<div  runat="server" id="DisplayText"></div>



In .cs page
C#
List<int> column0 = new List<int>();
using (Stream stream = File.Open(@"D:\def.txt", FileMode.Open))

using (TextReader sr = new StreamReader(stream, Encoding.UTF8))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        string[] arr = line.Trim().Split(',');
        foreach (var item in arr)
        {
            column0.Add(Convert.ToInt32(item));
        }
    }

    int[] intArray = column0.ToArray();
    if (intArray.Count() > 0)
    {
        foreach (var itemint in intArray)
        {
            Label display = new Label();
            display.Text = itemint.ToString() + ",";
            DisplayText.Controls.Add(display);
        }
    }
}
 
Share this answer
 
v2
Read the text into a string variable and use the Split() method to create an array of the individual elements. Then use something like Int32.Parse() to convert each element to an integer and store in a new array.
 
Share this answer
 
Read the content of the text file using the System.IO namespace and the File.ReadAllText() method and store it in a string variable. Split the string by (,). Then loop through the string using for.. loop construct and fetch it into an integer array as you wanted.

Hope this help. If you have any addition help try to post what you have accomplished so far then, brothers can look at what you are not doing right and assist you.
 
Share this answer
 
There are a couple of ways to do it:
1) Read the file as a string, and use string.Split to break it into numbers:
C#
string s = File.ReadAllText(path);
string[] numbers = s.Split(',');
You can then loop through each number and use int.Parse to convert the string value into an integer, and store it in an array or a List<int>
2) Treat the file as Comma Separated Values, and use one of the many converters available. Here is one: A Fast CSV Reader[^]

The first is simple to implement, the second takes a little more work. But it's worth considering the second, because it may be easier to use in the future, should your data change for any reason.
 
Share this answer
 
string str= File.ReadAllText(path);
int[] result=new int[100];
for (int i = 0; i < str.Split(',').Length; i++)
{
result[i] = Convert.ToInt32(str.Split(',')[i]);


}
 
Share this answer
 
v2
C#
List<int> column0 = new List<int>();
                using (Stream stream = File.Open(@"C:\def.txt", FileMode.Open))
                using (TextReader sr = new StreamReader(stream, Encoding.UTF8))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                            {
                                 string[] arr = line.Split(',');
                                 ....................
                                 // how do i code here


                            }
 
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