C
C#10mo ago
Dinny

✅ c# input and output files

So I am trying to design a code that calculates the area of a triangle giving the values in an input file. the answer will appear in the output file and “invalid” if imported values are entered. the code isn’t crashing, but nothing appears in the output file and idky
No description
8 Replies
mtreit
mtreit10mo ago
What type is writer? Make sure you dispose it before the program exits to ensure it flushes to disk. Assuming it's a StreamWriter Usually the best practice for that is to use using to ensure it gets disoposed when it goes out of scope.
using System;
using System.IO;

var file = @"c:\temp\foo.txt";

Console.WriteLine("Let's write the file!");

using (var sw = new StreamWriter(file))
{
sw.WriteLine("Foo");
sw.WriteLine("Bar");
} // sw goes out of scope here and the data is flushed to disk.

Console.WriteLine("Let's read the file!");

// Alternate syntax, will be disposed when we reach the end of the function, in this case
// when the program exits because we are in the Main function.
using var sr = new StreamReader(file);

while (sr.ReadLine() is string line)
{
Console.WriteLine(line);
}
using System;
using System.IO;

var file = @"c:\temp\foo.txt";

Console.WriteLine("Let's write the file!");

using (var sw = new StreamWriter(file))
{
sw.WriteLine("Foo");
sw.WriteLine("Bar");
} // sw goes out of scope here and the data is flushed to disk.

Console.WriteLine("Let's read the file!");

// Alternate syntax, will be disposed when we reach the end of the function, in this case
// when the program exits because we are in the Main function.
using var sr = new StreamReader(file);

while (sr.ReadLine() is string line)
{
Console.WriteLine(line);
}
outputs:
[16:43:37] ✓ dotnet run
Let's write the file!
Let's read the file!
Foo
Bar
[16:43:37] ✓ dotnet run
Let's write the file!
Let's read the file!
Foo
Bar
Dinny
Dinny10mo ago
oh yeah it’s stream writer, it’s above the code presented, my mistake tho i forgot to close the files so it was an endless loop 😭😭 i’m so embarrassed to have such a simple error ty for ur help :3
Mayor McCheese
Mayor McCheese10mo ago
Get used to that Simple errors trip you up the most
Dinny
Dinny10mo ago
i spent two hours looking for an error when i just didn’t a semicolon 😭😭
JakenVeina
JakenVeina10mo ago
I've done equivalent things, recently it'll never stop, no one's gonna judge you for it get yourself a drink and move on 😉
ffmpeg -i me -f null -
wasn't that reported as an error by the ide
Dinny
Dinny10mo ago
i didn’t bother to look at the bottom part of the console to see errors listed 🥲🥲 it was my first time writing a program too so i was in my head w everything the only reason i noticed was bc i jotheeed to look down bothered lmao
Accord
Accord10mo 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
More Posts
❔ Defining base impl ctor in interfaceSo currently for some testing I have the following code: ```csharp interface HumanInterface ❔ Weird WPF exceptioni get this stupid exception whenever i create a new page or window. this shows up as soon as i call ❔ I get Invalid ModelType because my hidden field is emptySo I have a field (Unit of measurement) that will be hidden if the user chooses the option ''Check-i❔ Confusion as to what to default to when it comes to accessibility modifiersSo I come from a extensive C++ background, in which we use the keywords: `public` and `private` a bi❔ How can I make it so an array saves changes when I turn off the programBasically I'm making an array of strings that can be removed or added to - how can I make it so if s✅ C# null exception with SQLI was trying to deploy my application to another computer to try it, and I got an error where it cou❔ Problem with getting GPU infoI'm currently working on a console program that shows some information of user's PC for fun. I have ✔ Simple code structure questionLet's say I have interface "IShapes" in file Shapes.cs Now, I'd like to create 3 more classes. Circl❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFII have the following: ```cs public sealed unsafe partial class Test : IDisposable { public ❔ How do I hide or show a div based on the value of a property of my selected option?When I select a habit type, that habit type has a Measurability property, and I want to show or hide