Home > OS >  Writing to same csv line twice?
Writing to same csv line twice?

Time:04-11

I am attempting to create code that will write values into a csv file, do some other unrelated code and then later on add an extra value to that line

    static void Main(string[] args)
    {
        int newNumber = 110;
        var records = new List<MovieInfo>
        {
            new MovieInfo {Rating = newNumber, Price = 44},
        };

        var config = new CsvConfiguration(CultureInfo.InvariantCulture)
        {
            HasHeaderRecord = false,
        };

        string filePath = @"C:\file\path\go\brrrr";
        
        using (var stream = File.Open(filePath, FileMode.Append))
        using (var writer = new StreamWriter(stream))
        using (var csv = new CsvWriter(writer, config))
        {
            csv.WriteRecords(records);
        }

        //--------------------------------------
        //other unrelated code things happen here
        //--------------------------------------

        double value = 4;
        string thirdValue = ","   value;
        File.AppendAllText(filePath, thirdValue);
    }
    public class MovieInfo
    {
        public int Rating { get; set; }
        public int Price { get; set; }
    }

The output of this code is this

Output code

Is it possible to adjust the code so that the output will be 110,44,4 instead? Additional info if it is relevant would be that the line in question will always be the last in the file. Any help is appreciated

CodePudding user response:

When you call csv.WriteRecords(records); all records are written to the output file and each record is separated by a newline, even the last record has a new line appended.

Instead of writing every record in one go, you could write a record at time with the WriteRecord method. This method doesn't write the newline.
A new line is added when you call the NextRecord method. So looping on your records minus one you can control the output of the newline on the last line:

void Main()
{
    int newNumber = 110;
    var records = new List<MovieInfo>
    {
        new MovieInfo {Rating = 200, Price = 30},
        new MovieInfo {Rating = newNumber, Price = 44},
    };

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        HasHeaderRecord = false,
    };

    string filePath = @"E:\temp\movieinfo.csv";

    using (var stream = File.Open(filePath, FileMode.Append))
    using (var writer = new StreamWriter(stream))
    using (var csv = new CsvWriter(writer, config))
    {
        // Write all records minus one.
        for (int x = 0; x < records.Count - 1; x  )
        {
            csv.WriteRecord(records[x]);
            csv.NextRecord();
        }

        // Write the last record without the newline
        csv.WriteRecord(records[records.Count-1]);
    }

    //--------------------------------------
    //other unrelated code things happen here
    //--------------------------------------

    double value = 4;
    string thirdValue = ","   value;
    File.AppendAllText(filePath, thirdValue);

}
  • Related