C
C#2y ago
idris_2020

❔ How would I replace a CSV file line by line with another array?

I have a 6x8 CSV file full of 0s and 1s, and I want to replace the CSV file with another 2D array that is also 6x8
29 Replies
Anton
Anton2y ago
just characters? or a bit array?
idris_2020
idris_20202y ago
they are characters
Anton
Anton2y ago
what's the problem?
idris_2020
idris_20202y ago
how do i replace the csv file with a 2d array
Anton
Anton2y ago
the easiest way would be to read it fully, then rewrite it with the new content
idris_2020
idris_20202y ago
yeah i can do that and replace it with a 1D array, but idk how with an 2D array for 1D i do File.WriteAllLines("file.csv", lines);
Anton
Anton2y ago
you could just
string csv = File.ReadAllText(filename);
string array = csv.Replace(",", "");
File.WriteAllText(filename, array);
string csv = File.ReadAllText(filename);
string array = csv.Replace(",", "");
File.WriteAllText(filename, array);
or the same for lines, you'd replace the commas with empties for each line
idris_2020
idris_20202y ago
and how to replace with a 2D array
Anton
Anton2y ago
or split on commas, check if they are in fact 1 or 0, the string.join them back what's that mean?
idris_2020
idris_20202y ago
ill give an example
idris_2020
idris_20202y ago
here is my csv file (in notepad)
idris_2020
idris_20202y ago
i want to replace it all with a 2D array that is also 6x8
idris_2020
idris_20202y ago
eg
Anton
Anton2y ago
that's not replacing with an array, that's parsing it as an array replacing would mean writing to disk in this case you can't write that to disk
idris_2020
idris_20202y ago
i mean it is a file and i do want to replace it then save it
Anton
Anton2y ago
you want to save those as an array literal? into a file? may I ask why? that just isn't something you'd ever do, because it makes no practical sense
idris_2020
idris_20202y ago
ok so basically i have a cinema booking program and in the 6x8 array 0s represent empty seat and 1s represent a taken seat. While the user is booking seats the cinema will have some seats taken, then i want to save that array onto a csv file
Anton
Anton2y ago
so do you need to parse the csv into an array, or to format the array contents as a csv?
idris_2020
idris_20202y ago
format the array contents as a csv
Anton
Anton2y ago
well then you need to join each line with commas with string.Join if your array is a bool array, then convert each element to a string "0" or "1" first
idris_2020
idris_20202y ago
its not bool theyre strings
Anton
Anton2y ago
then you need that string.Join, or use a for loop with a string builder and add the commas manually
idris_2020
idris_20202y ago
how would i be able to do that
Anton
Anton2y ago
you call that function for every row
idris_2020
idris_20202y ago
so like string.Join(array[0..7]) but how for a 2D array
Anton
Anton2y ago
do that for every line then write the lines into the file
idris_2020
idris_20202y ago
idrk how to do that
Anton
Anton2y ago
look up how to use for loops
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
More Posts
❔ How do I resolve a 'package downgrade' if one of my dependencies needs an older version of a dll?I'm developing an app that uses the newest EntityFramework Core 7 (.NET 7) to communicate with its o❔ Will I get a performance hit if I use the Enum type as a key value for a Dictionary?See Code below: ``` public enum Tee1 { None } public enum Tee2 { None } class Program { ✅ Design decision over multiple inserts in a single commandHi, any design suggestions on how to handle an update command from a mvc form which update needs to ❔ Create a HostedService using BackgroundService to listen to requestsHello, I am currently trying to build a BankAccount project and I wrote my business logic. My next ❔ When Vpn connectivity is off don't crash the app.I have built this logic it does make a popup appear with a message about vpn not being connected if ❔ Update-Database errorGetting the following error with the following migration, is anyone able to tell why? https://pasteb❔ PDF generation library?I'm looking for a way to generate PDF file in an ASP.NET application.❔ Entity Framwork SQLite AddHello, i've a little problem there. I'm trying to add something new in my database but i do not unde❔ Linq group by with multiple columns for the outer and then 1 other column for the inner groupLets say I have a list of class Car. It has properties Id, Model, Year, Age,CarCode. A carCode is un❔ Following BPM with codeI’m wanting to create my own very small dmx light show maker in c#. I realize it takes **forever** t