0

My problem is to save and read the file.

Can u suggest the best way to save the data(mostly numbers) from various forms into a data file (can be .txt, .dat etc) and then read a specific data from the file...

For example..

I write:

Data 1 Data 2 Data 3

when in the equation need data 3, it can read it and pick data 3 only...

What I'm thinking of right now is to save data per line,

For example:

Data 1
Data 2
Data 3

So when I need data 3, I just need to pick data from 3rd line to pick data 3.

How to do it?

I've researched a little and found this

string[] Lines = File.ReadAllLines("filename.txt");
Lines[1] = "second line new value";
File.WriteAllLines("filename.txt");

from what I get, that command will write data on second line at filename.txt. If its true, how to read it?

Is there a better way to do it?

I don't mind if u just paste a url for me to read or directly post a sample code.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
  • above code will not compile at all, you should at least find some working code first. by the way another way is xml/json serialization – Lei Yang Aug 02 '17 at 01:18

2 Answers2

0

Try this:

Ir reads the contents of a text file by using the static methods ReadAllText and ReadAllLines from the System.IO.File class.

// Read the file as one string.
                string text = System.IO.File.ReadAllText(@"C:\WriteText.txt");

       // Display the file contents to the console. Variable text is string.
                System.Console.WriteLine("Contents of WriteText.txt = {0}", text);

    // Read each line of the file into a string array. 
    // Each element of the array is one line of the file.
             string[] lines = System.IO.File.ReadAllLines(@"C:\WriteLines2.txt");

                    // Display the file contents by using a foreach loop.
                    System.Console.WriteLine("Contents of WriteLines2.txt = ");
                    foreach (string line in lines)
                {
                    // Use a tab to indent each line of the file.
                    Console.WriteLine("\t" + line);
                }
                // Keep the console window open in debug mode.
                 Console.WriteLine("Press any key to exit.");
                 System.Console.ReadKey();
Tehmina
  • 205
  • 2
  • 8
0

This is my solution for that kind of problem, when I want to save some data in a .txt configuration file and then I want to retrieve that information.

This is the WriteToFile method, it also checks if the data that you are inserting is on the file already:

 using System.IO;

 public void WriteToFile(string newData, string _txtfile)
    {
        List<string> ListToWrite = new List<string>();
        /// Reads every line from the file
        try
        {
            using (StreamReader rd = new StreamReader(_txtfile, true))
            {
                while (true)
                {
                    ListToWrite.Add(rd.ReadLine().Trim());
                }
            }
        }
        catch (Exception)
        {
        }

        try
        {
            /// Check if the string that you want to insert is already on the file
            var x = ListToWrite.Single(a => a.Contains(newData));

            /// If there's no exception, means that it found your string in the file, so lets delete it.
            ListToWrite.Remove(x);

        }
        catch (Exception)
        {
            /// If a exception is thrown, it did not find your string so add it.
            ListToWrite.add(newData);
        }

        /// Now is time to write the NEW file.
        if (ListToWrite.Count > 0)
          {
              using (StreamWriter tw = new StreamWriter(_txtfile, true))
              {
                  try
                  {
                      foreach (string s in l)
                      {
                          tw.WriteLine(s);
                      }
                  }
                  catch (Exception)
                  {
                      break;
                  }
              }
          }
    }

Now if you want to retrieve some information by using a string to search:

    using System.IO;

    public static string GetData(string _txtfile, string searchstring)
    {
        string res = "";
        using (StreamReader rd = new StreamReader(_txtfile, true))
        {
            while (true)
            {
                try
                {
                    string line = rd.ReadLine().Trim();
                    if (line.Contains(searchstring))
                    {
                        return line;
                    }

                }
                catch (Exception)
                {
                    break;
                }
            }
        }
        return res;
    }

You can tweak this and make it way better, but this is currently working for me.