Read Text file and access elements of Text file separately in c# -


i reading text file consisting of 2 columns (contains x , y coordinates):

static void main(string[] args)     {         string line;         using (streamreader file = new streamreader(@"c:\point(x,y).txt"))         {             while ((line = file.readline()) != null)             {                  char[] delimiters = new char[] { '\t' };                 string[] parts = line.split(delimiters, stringsplitoptions.removeemptyentries);                 (int = 0; < parts.length; i++)                 {                     console.writeline(parts[i]);                 }              }          }         // suspend screen.         console.readline(); 

i want read data in text file separately in 2 different variables how access each value?

this case linq can come rescue. can read lines , parse directly linq. assuming have tab delimited text file based on line.split doing. can create new anonymous type capture row/line in text file. have access fields seen when writing out console, looping through each item/anonymous type in list. make sure have using system.linq namespace included there default.

var records = file             .readalllines(@"c:\users\saira\documents\visual studio 2017\projects\point(x,y).txt")             .select(record => record.split('\t'))             .select(record => new             {                 x = record[0],                 y = record[1]             }).tolist();          foreach (var record in records)         {             console.writeline($"the x coordinate is:{record.x}, , y coordinate {record.y}");         } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -