C#
C#

help

Root Question Message

Idris20h
Idris20h12/16/2022
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
AntonC
AntonC12/16/2022
just characters? or a bit array?
Idris20h
Idris20h12/16/2022
they are characters
AntonC
AntonC12/16/2022
what's the problem?
Idris20h
Idris20h12/16/2022
how do i replace the csv file with a 2d array
AntonC
AntonC12/16/2022
the easiest way would be to read it fully, then rewrite it with the new content
Idris20h
Idris20h12/16/2022
yeah i can do that and replace it with a 1D array, but idk how with an 2D array
Idris20h
Idris20h12/16/2022
for 1D i do File.WriteAllLines("file.csv", lines);
AntonC
AntonC12/16/2022
you could just
string csv = File.ReadAllText(filename);
string array = csv.Replace(",", "");
File.WriteAllText(filename, array);
AntonC
AntonC12/16/2022
or the same for lines, you'd replace the commas with empties for each line
Idris20h
Idris20h12/16/2022
and how to replace with a 2D array
AntonC
AntonC12/16/2022
or split on commas, check if they are in fact 1 or 0, the string.join them back
AntonC
AntonC12/16/2022
what's that mean?
Idris20h
Idris20h12/16/2022
ill give an example
Idris20h
Idris20h12/16/2022
here is my csv file (in notepad)
Idris20h
Idris20h12/16/2022
i want to replace it all with a 2D array that is also 6x8
Idris20h
Idris20h12/16/2022
eg
AntonC
AntonC12/16/2022
that's not replacing with an array, that's parsing it as an array
AntonC
AntonC12/16/2022
replacing would mean writing to disk in this case
AntonC
AntonC12/16/2022
you can't write that to disk
Idris20h
Idris20h12/16/2022
i mean it is a file
Idris20h
Idris20h12/16/2022
and i do want to replace it
Idris20h
Idris20h12/16/2022
then save it
AntonC
AntonC12/16/2022
you want to save those as an array literal?
AntonC
AntonC12/16/2022
into a file?
AntonC
AntonC12/16/2022
may I ask why?
AntonC
AntonC12/16/2022
that just isn't something you'd ever do, because it makes no practical sense
Idris20h
Idris20h12/16/2022
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
AntonC
AntonC12/16/2022
so do you need to parse the csv into an array, or to format the array contents as a csv?
Idris20h
Idris20h12/16/2022
format the array contents as a csv
AntonC
AntonC12/16/2022
well then you need to join each line with commas with string.Join
AntonC
AntonC12/16/2022
if your array is a bool array, then convert each element to a string "0" or "1" first
Idris20h
Idris20h12/16/2022
its not bool theyre strings
AntonC
AntonC12/16/2022
then you need that string.Join, or use a for loop with a string builder and add the commas manually
Idris20h
Idris20h12/16/2022
how would i be able to do that
AntonC
AntonC12/16/2022
you call that function for every row
Idris20h
Idris20h12/16/2022
so like
Idris20h
Idris20h12/16/2022
string.Join(array[0..7])
Idris20h
Idris20h12/16/2022
but how for a 2D array
AntonC
AntonC12/16/2022
do that for every line
AntonC
AntonC12/16/2022
then write the lines into the file
Idris20h
Idris20h12/16/2022
idrk how to do that
AntonC
AntonC12/16/2022
look up how to use for loops
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy