Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to convert File.ReadLines into string array?

Tags:

c#

file

The question that I have is regarding converting the process of reading lines from a text file into an array instead of just reading it.

The error in my codes appear at string[] lines = File.ReadLines("c:\\file.txt"); with cannot implicity convert....

Can someone please advise on the codes to save the results in an array format? I've placed the ReadAllLines code which is able to save the results in an array too. Thanks!

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO;  namespace Testing { class Analysis {     static void Main()     {         string[] lines = File.ReadLines("c:\\file.txt");          foreach (string r in lines)         {             Console.WriteLine("-- {0}", r);         }          // Keep the console window open in debug mode.         Console.WriteLine("Press any key to exit.");         System.Console.ReadKey();     } } } 

ReadAllLines Codes:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO;  namespace Testing { class ReadFromFile {     static void Main()     {         string[] lines = System.IO.File.ReadAllLines         (@"C:\Users\Public\TestFolder\WriteLines2.txt");          System.Console.WriteLine("Contents of writeLines2.txt =:");         foreach (string line in lines)         {             Console.WriteLine("\t" + line);         }          // Keep the console window open in debug mode.         Console.WriteLine("Press any key to exit.");         System.Console.ReadKey();     } } } 
like image 286
JavaNoob Avatar asked Nov 19 '10 00:11

JavaNoob


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

File.ReadLines() returns an object of type System.Collections.Generic.IEnumerable<String>
File.ReadAllLines() returns an array of strings.

If you want to use an array of strings you need to call the correct function.

You could use Jim solution, just use ReadAllLines() or you could change your return type.

This would also work:

System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt"); 

You can use any generic collection which implements IEnumerable. IList for an example.

like image 190
Gerald Davis Avatar answered Sep 27 '22 22:09

Gerald Davis


string[] lines = File.ReadLines("c:\\file.txt").ToArray(); 

Although one wonders why you'll want to do that when ReadAllLines works just fine.

Or perhaps you just want to enumerate with the return value of File.ReadLines:

var lines = File.ReadAllLines("c:\\file.txt"); foreach (var line in lines) {     Console.WriteLine("\t" + line); } 
like image 33
Jim Mischel Avatar answered Sep 27 '22 20:09

Jim Mischel