❔ 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
just characters? or a bit array?
they are characters
what's the problem?
how do i replace the csv file with a 2d array
the easiest way would be to read it fully, then rewrite it with the new content
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);
you could just
or the same for lines, you'd replace the commas with empties for each line
and how to replace with a 2D array
or split on commas, check if they are in fact 1 or 0, the string.join them back
what's that mean?
ill give an example
here is my csv file (in notepad)
i want to replace it all with a 2D array that is also 6x8
eg
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
i mean it is a file
and i do want to replace it
then save it
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
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
so do you need to parse the csv into an array, or to format the array contents as a csv?
format the array contents as a csv
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" firstits not bool theyre strings
then you need that
string.Join
, or use a for loop with a string builder and add the commas manuallyhow would i be able to do that
you call that function for every row
so like
string.Join(array[0..7])
but how for a 2D array
do that for every line
then write the lines into the file
idrk how to do that
look up how to use for loops
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.