Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 30-Apr-13 23:53pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

In .cs page
            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);
                    }
                }
            }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

 
                            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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]);
 

}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 432
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,632
1 Sergey Alexandrovich Kryukov 5,404
2 Maciej Los 3,474
3 Peter Leow 3,299
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100