-1

I'm working on my school project and have this problem with data type. At first i'm writing data to the txt file, then i want to read it in order to sort the data. But it seems impossible to do that with string. Here is some code if necessary

string fileName = "highscore.txt";
                int textToAdd = Score;
                using (StreamWriter writer = new StreamWriter(fileName, true))
                {
                    writer.Write(textToAdd+"\n");

                }
                using (StreamReader reader = new StreamReader(fileName, true))
                {
                    

                }

The text contains multiple numbers separated by spaces, eg :

1 2 3 

I want to read the numbers and sort them.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
LeeZee
  • 7
  • 1
  • 1
    Can I suggest writing (and reading) JSON instead? – mjwills Jan 14 '21 at 10:25
  • any advices how to do that ? i mean would it be okay or an total overkill. No experience with json at all :( – LeeZee Jan 14 '21 at 10:27
  • Are you storing just a single number to file? – Chetan Jan 14 '21 at 10:27
  • [Read and parse a Json File in C#](https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp) – Liam Jan 14 '21 at 10:27
  • Not really, it's done a few times. So i will have something like 1 2 3 and then i have to sort that – LeeZee Jan 14 '21 at 10:27
  • A text file contains characters, not numbers. A stream only works with bytes though. A StreamReader converts bytes to characters or strings. Why do you want to cast the character returned by `StreamReader.Read` to an int? Or the `byte` returned by `Stream.Read()`? If you want to read an entire line use `ReadLine`. If you want to read all the text, use. `ReadToEnd` – Panagiotis Kanavos Jan 14 '21 at 10:28
  • 1
    Thanks @Liam i will try that – LeeZee Jan 14 '21 at 10:29
  • 1
    https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp from @Liam is a great starting point. File parsing is hard. JSON processing is much easier. – mjwills Jan 14 '21 at 10:29
  • @PanagiotisKanavos in order to sort them to have the one with the highest value – LeeZee Jan 14 '21 at 10:30
  • The solution approach depends on what and how you are storing in the file. If you are storing one line with space separated numbers then you need to read line from the file, split it by space character and then convert items of that array to number and then sort – Chetan Jan 14 '21 at 10:31
  • 1
    Does this answer your question? [Read and parse a Json File in C#](https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp) – Liam Jan 14 '21 at 10:35
  • @Liam Working on it – LeeZee Jan 14 '21 at 10:36
  • @LeeZee you don't need JSON to work with text files. You didn't mention you stored integers *in the question itself*, or that you wanted to order them. `Char` can be explicitly converted to its Unicode value, so `converting to int` is unclear. Besides, `10` is *two* characters, and `Read` returns one character at a time – Panagiotis Kanavos Jan 14 '21 at 10:44
  • @LeeZee to read and parse numbers from a text file, read one line at a time with `var line=reader.ReadLine()`, split it with `line.Split(" ")`, then parse the strings with `int.Parse()`. Reading one character at a time is more efficient but more complicated as well – Panagiotis Kanavos Jan 14 '21 at 10:47
  • Thanks guys,i think i got this :) – LeeZee Jan 14 '21 at 11:02

1 Answers1

1

I hope below snippet helps you!

using (StreamReader reader = new StreamReader(fileName, true))
{
     int value = Convert.ToInt32(reader.ReadToEnd());
}
ilyas varol
  • 788
  • 3
  • 10
  • 25