How to read a text file from a server and store in a dictionary using C #

I have to get a text file from the server and then read the file and save it in the dictionary on the client.

I read the file, but I'm not sure how to extract the information from the file and save it in the dictionary from here.

My text file contains the following data:

Amy; 10.30; one hundred; $ 7

Bandy; 4.30; one hundred; $ 9

Bobby; 3.20; 80; $ 7

Client code

private static void loadMovies() { try { byte[] data = new byte[1024]; writer.WriteLine(BROWSE); writer.Flush(); while (true) { data = ReceiveMovieData(server); MemoryStream ms = new MemoryStream(data); loadMovies(); break; } } catch (Exception ex) { //textBox1.Text = ex.Message; } } private static byte[] ReceiveMovieData(Socket s) { int total = 0; int recv; byte[] datasize = new byte[4]; recv = s.Receive(datasize, 0, 4, 0); int size = BitConverter.ToInt32(datasize, 0); int dataleft = size; byte[] data = new byte[size]; while (total < size) { recv = s.Receive(data, total, dataleft, 0); if (recv == 0) { break; } total += recv; dataleft -= recv; } return data; } 
+4
source share
1 answer

I assume your data is separated by a comma ; , and each entry is divided by an interrupt string. So my suggestion is to analyze the data:

  • Reading each record in a row
  • Separation of columns by ; (semicolon). There are methods when your data is already processed by a string as follows:

     string s = "abc; 123"; string[] columns = s.Split(';'); 
  • Then correctly dropping each column into the corresponding data type.

  • And then save it in a custom Dictionary that will meet your requirement

0
source

All Articles