How do i create a external file containing previously entered strings?
My program (a gtin verifier) asks the user to enter a gtin, which it saves as a string. After that the Method returns a true or false, depending on if the code is valid or not. for efficiency's sake i want it to save every input of the user and the true/false result to a file so if someone enters a code that was already calculated it just reads the result from the file instead of having to acutally calculate it, i'll send the code in a bit
31 Replies
the language for the user is german but that should not matter:
Why not save it into a list, or some other collection?
Does it need to be in a file on the disk?
don't really care what it's called nor where it is, it just needs to be persistent across restarts of the program
You should care. This it's your program
If you don't care, why should we?
If it needs to persist through restarts, then yes, a file is what you want
There are databases which can do this for you, but plain text files work fine too
i do care a bit, tho as long as the result is the same i don't really care with wich method someone recommends me to save it with, as long as it means i don't need to re-write my program
Look into File.AppendText and File.ReadLines
a whole database seems overkill, i'd imagine something like that:
or anything like that
❯ Method: System.IO.File.AppendText(String)
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
React with ❌ to remove this embed.
❯ Method: System.IO.File.ReadLines(String)
Reads the lines of a file.
❯ Method: System.IO.File.ReadLines(String, Encoding)
Read the lines of a file that has a specified encoding.
❯ Method: System.IO.File.ReadLinesAsync(String, CancellationToken)
Asynchronously reads the lines of a file.
3/4 results shown ~ Click Here for more results
React with ❌ to remove this embed.
You could for example store all your info for your entry in one line
Then you'd
File.ReadLines(...).Select(...)
, and in the Select, Split()
the line on some delimiteri'm not really following, i'll try something
Then parse each split value and deserialize that into some real collection or object
Another idea is using an actual file format like xml or json
Using those you can use the JsonSerializer and XmlSerializer classes
that just confused me more...
i'd imagine something like this:
What does the file store again? The calculated value?
And what type does Gtin.Calculate return?
A bool
Hm, you're storing a bool to the file?
The the gtin-code itself and the result
Hm. Then would it make more sense only to store the code?
And to only store the code of those which are valid codes?
Not really, I'd like it to store whatever the user enters
Hm, right
Do you have an example input?
12345678,1.
For example, the"," is the seperator from string to bool and the dot is the end of that entry, just chose them at the moment
I meant more the user input
Oh
The input is simply a 8, 13 or 14 digit number as a string
The method for the calculation returns the bool
And I'll be gone for about half an hour, do you still need any info?
That's fine
Or something
And then call
TryCalculateGtin()
on your ReadLine, and then do something when that returns false if you want (tell the user the code isn't valid or what have you)
that was a little bit more than half an hour, but i'm back
does not really work, can i call you and stream my display?
god no lmao
i wrote this completely on my phone
oh... why
so i'm not surprised that it doesn't work
because i'm not home
oh
discord shows me a online desktop client for your profile... nvm then, maybe later
i'm at work now lol
at the time i was on my phone
it's okay, it does not need to be done now or tomorrow, it's just for efficiencies sake so it works without that, just not as efficient