2

I would like to take a text file with a lot of lines and turn it into an array. For example if the text file was:

Line 1

Line 2

Line 3

Line 4`

It would result in

string[] stringList = { "Line 1", "Line 2", "Line 3", "Line 4" };

How do I do this?

I've tried this:

string line;
string[] accountList;
            using (StreamReader file = new StreamReader(accountFileLocation.Text))
            {
                while (line = file.ReadLine() != null)
                {
                    stringList += line;
                }
            }

However that errors with:

Cannot implicitly convert type 'bool' to 'string'

Cannot convert type 'string' to 'string[]'

Cannot implicitly convert type 'string' to 'string[]'

Community
  • 1
  • 1
Jon
  • 2,566
  • 6
  • 32
  • 52
  • 5
    By learning how to code and then applying your knowledge. What have you tried? – Ant P Dec 01 '13 at 22:47
  • Forgot to add what I tried to the post, added it. – Jon Dec 01 '13 at 22:48
  • possible duplicate of [What's the fastest way to read a text file line-by-line?](http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line) – ebram khalil Dec 01 '13 at 22:57

4 Answers4

7

just use

  string[] lines =  File.ReadAllLines(yourpathFile);  
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • Will that automatically put them into a different string for each line? Or just one big string? – Jon Dec 01 '13 at 22:48
  • each line terminate with \r\n will be a different item in the array – BRAHIM Kamel Dec 01 '13 at 22:49
  • There's a lot of lines (a few thousand) in the text file. Is there a way to do it without \r\n? – Jon Dec 01 '13 at 22:50
  • can you explain more please – BRAHIM Kamel Dec 01 '13 at 22:51
  • So I want each line in a text file (where there are a few thousand lines) to an individual string in the array. Is there a part you don't understand? – Jon Dec 01 '13 at 22:52
  • ok this method will work fine even if you have a thousands of lines – BRAHIM Kamel Dec 01 '13 at 22:53
  • So do I have to add \r\n to the end of every line? There's too many lines to go through and add that to every line. – Jon Dec 01 '13 at 22:54
  • @chipperyman573 No `\r\n` indicates the end of the line, so basically if you see them as seperate lines in notepad, then they end with a newline already. – Silvermind Dec 01 '13 at 22:55
  • normally you don't see \r\n if it a multilines file it will terminate like this it's a normal behavior you don't need to add anything – BRAHIM Kamel Dec 01 '13 at 22:55
  • There is also File.ReadLines which is lazily enumerated line by line, not to forget. Should be better for large files. – nawfal Dec 19 '13 at 17:40
0

I'll walk you through how I came across the solution as learning what to Google is an important skill in finding solutions.

If you Google 'How to open a text file in c#' you find this webpage http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx which uses a class called 'StreamReader' and its method 'ReadToEnd()' in an example it gives you.

If you then go on to Google 'c# StreamReader' you can find all of its methods that it has http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx

If you scroll down to the R methods you can find one called 'ReadLine'. By clicking into it you can find a working example of exactly what you need: http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline(v=vs.110).aspx

string path = @"c:\temp\MyTest.txt";
 using (StreamReader sr = new StreamReader(path)) 
        {
            while (sr.Peek() >= 0) 
            {
                Console.WriteLine(sr.ReadLine());
            }
        }
user819640
  • 250
  • 5
  • 14
0

Could you try this:

StreamReader reader = new StreamReader();
String content = file.readToEnd();
file.close();
String[] lines = content.split("\n".toCharArray());
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • This would leave `\r` at the end of every line when it is in Windows format, even so it is not very smart to read everything in a sinlge string and then split it again. – Silvermind Dec 01 '13 at 22:59
0

just try :

string n = "line1\r\nline2\r\n";
string[] list = n.Split('\r'); //or string[] list = n.Split('\n')