❔ Help with code

Hello i am a bit of a beginner in c# and i would like to get some guidance with some work i have from school . So basically i have this little line of code
using System; using System.Collections.Generic; using System.IO; namespace SelectionCandidat { class Program { static void Main(string[] args) { /* Get data from input file */ var data = new List<String>(); string line; while ((line = Console.ReadLine()) != null) { data.Add(line); } // ADD YOUR CODE HERE } } } and basically on the software we can run different text files and the output should be a txt file too but i don't know how to interact with the data from the txt files that inside look like this 5 0,0 3 r u u So basically the first line is the size of the grid x,y Second line is the coordinates on that grid first is x second is y Third line is the number of instructions and the last lines are the instuctions that move though the grid so u is up and y does +1 d is for down and y does -1 l l or Left is -1 to x and r or right is +1 to x . i know how to do it with the terminal but with the txt files i am having an issues . Thanks for your time .
16 Replies
mtreit
mtreit2y ago
What part are you having trouble with? You probably want to use int.TryParse to turn the first three strings into numbers.
Potato_Boy🦉
Potato_Boy🦉OP2y ago
well i think i am having issues transforming the data collected from the txt files by the first lines of code to data that i can use and work on does int.TryParse work like that ?
mtreit
mtreit2y ago
Do you know how to read the data from the text file?
Potato_Boy🦉
Potato_Boy🦉OP2y ago
well i don't really know i think the files can be run on the software but then i don't know is it Console.ReadLine ? i am sorry but i am a bit of begginner i have a lot to learn
mtreit
mtreit2y ago
Console.ReadLine waits for someone to type text into the console and then press <Enter> and then whatever they typed is returned to your code as a string. If you need to read text files you won't use that. You will use something like this:
var data = File.ReadAllLines(@"c:\temp\data.txt");
var data = File.ReadAllLines(@"c:\temp\data.txt");
(For very large files you might not want to call ReadAllLines but in your scenario it sounds like the files are small.)
Potato_Boy🦉
Potato_Boy🦉OP2y ago
okay thanks i am gonna try it but can i ask what does this do then /* Get data from input file */ var data = new List<String>(); string line; while ((line = Console.ReadLine()) != null) { data.Add(line);
mtreit
mtreit2y ago
BTW, use $code to write code here
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
mtreit
mtreit2y ago
$codegif
mtreit
mtreit2y ago
That code you showed continually reads input from the console into a List of strings until the user presses ctrl+Z The comment that says it is reading from an input file seems to be wrong, unless we are using a very loose definition of the word file here.
Potato_Boy🦉
Potato_Boy🦉OP2y ago
Potato_Boy🦉
Potato_Boy🦉OP2y ago
because i can run the files on coding room but i am not sure if its right
mtreit
mtreit2y ago
I am not familiar with that site. It's possible when you select a file that it feeds the input to your code as if a user had typed them at the console?
Potato_Boy🦉
Potato_Boy🦉OP2y ago
i don't know if that works like that because i made a code before where i had the though the terminal but the work wants it to work though txt files so my work was wrong but anyways thank you for your help i am gonna try it for now and i'll get back if i have any issues
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server