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_2020OP2y ago
they are characters
Anton
Anton2y ago
what's the problem?
idris_2020
idris_2020OP2y 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_2020OP2y 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_2020OP2y 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_2020OP2y ago
ill give an example
idris_2020
idris_2020OP2y ago
here is my csv file (in notepad)
idris_2020
idris_2020OP2y ago
i want to replace it all with a 2D array that is also 6x8
idris_2020
idris_2020OP2y 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_2020OP2y 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_2020OP2y 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_2020OP2y 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_2020OP2y 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_2020OP2y ago
how would i be able to do that
Anton
Anton2y ago
you call that function for every row
idris_2020
idris_2020OP2y 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_2020OP2y 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