C
C#

help

❔ changing values in .csv file with file i/o commands

Ccynthialle1/18/2023
hello, i'm trying to replace a certain column in a csv file with file i/o commands but i can't quite find out how to
Ccynthialle1/18/2023
Here's an example of the .csv file
Ccynthialle1/18/2023
...
...
string[] doctors = File.ReadAllLines("Doctor.csv");
for (int i = 1; i < doctors.Length; i++)
{
string[] doctors2 = doctors[i].Split(',');
if (doctorID == doctors2[0])
{
//Not sure what to do here to replace values
}

}
...
...
string[] doctors = File.ReadAllLines("Doctor.csv");
for (int i = 1; i < doctors.Length; i++)
{
string[] doctors2 = doctors[i].Split(',');
if (doctorID == doctors2[0])
{
//Not sure what to do here to replace values
}

}
Ttimtier1/18/2023
You'd need to open a FileStream on this file to change the contents. Something like a StreamWriter ( or one of its implementations) would then allow you to write in it Though keep in mind that might become a bit messy considering you'll need to keep the same order of all entries to not mess with data integrity. Creating an entirely new file and removing/archiving the old one might be easier and safer unless needed
Ccynthialle1/18/2023
Would that be a better option? (the creating a new file one) also is there a way to directly replace a file with another file if i do it this way?
Ttimtier1/18/2023
Directly, maybe, not sure since im not behind a PC to google :p But basically: 1. Create new file. 2. Delete (or rename) old file 3. Rename new file to old name. 4. Delete old file if you havent yet.
Ccynthialle1/18/2023
ohh okay
Ttimtier1/18/2023
Id think its better as you can ensure the file is only replaced once your edited file is done, though it depends on your situation
Ccynthialle1/18/2023
i'll try that, thank you so much <3
Ttimtier1/18/2023
Np, good luck 😄
AAccord1/19/2023
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
✅ CORS: Cross-Origin Request BlockedHello, I'm trying to figure out how to configure CORS on my .NET Web Api Application. The Api Appli✅ How do I move a Window in WinUI 3 C#?Hey everyone! I have a window that is shaped like a taskbar, so it's {screenWidth} x 50px tall. Curr❔ VIsual Studio 2022 Spell CheckingWhen I just opened visual studio 2022 today, it randomly just started doing spell checking on my cod❔ Segmentation fault when running app on raspberry piI am trying to compile a .net app for a raspberry pi, and making a very simple gpio app, but i'm get❔ has somebody a c# code for the game solitärplease can somebody help me❔ ✅ For Hire[For Hire] [Full Time / Part Time] [Remote Work] 👍First priority is Credit 👍Perfect results withi✅ Moving from HttpWebRequest to HttpClientHello, I'm in need of help refactoring from HttpWebRequest(obsolete) to HttpClient. I've tried to do❔ Is there any way to test the rendering of a component in Blazor based on different screen sizes?I have a component that renders a table. It will render a different amount of rows based on screen ❔ Random Letter```cs using System; using System.IO; class GFG{ public static void Main(string[] args) { Rando❔ Trying to create my first non-tutorial ASP.net project running into small errorTLDR; **take a look at last question** I am using the tutorial model which has a page called "PrivHow to differentiate endpoints based on parameters?I want to have two endpoints `/users` and `/users?id=#`, but swagger doesn't seem to like what I ha❔ .exe dont workHello, when i send my executable to friend it dont works, but for me it works❔ Install several .NET versionsI have .NET 6 install on my Ubuntu pc and I would to have .NET 7 too. I install .NET 6 through Ubunt✅ Best way to get a response from an APINeeding to get a small image from an API response, im aware of multiple ways of doing this, is it beI want to extract data from discord for my application, how can I do it?I want to extract data from discord for my application, how can I do it?Is this the right way to save state of my ASP.NET/Angular app as a URL?I have an ASP.NET Core + Angular project. Once a file is uploaded to my ASP.NET API, a JSON is retuResizing an arrayI have declared what I think is an array of integers ``int[] validCoords = new int[1];`` I believe t❔ Get random element, even distributionany suggestion for even random item from list? i want to avoid situation where i got 10 times item Idk how class worksI'm working with creating the functions in a separate file, so I wanted to create a function that woAvoiding 'new' in IOCHello, I am learning about dependency injection and one of the things said to avoid is using `new`