JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.76k stars 1.07k forks source link

no errors, zero length file #804

Closed DrYSG closed 7 years ago

DrYSG commented 7 years ago

I have a dirt simple application that is using the WriteRecord() method to dump a public C# class with public accessors for the properties.

Oddly, this only works in version 2.16.3, in versions after that, I see that the written file length is zero, even though I an cleanly exiting and flushing the file.

I assume it is something simple, and stupid that I am doing. Right?

    class SaveBot
    {
        StreamWriter logFile;
        string name;
        CsvWriter csv;
        public SaveBot(string entityName)
        {
            name = entityName;
            logFile = new StreamWriter(name + ".csv");
            csv = new CsvWriter(logFile);
            csv.WriteHeader<Entry>();
        }
        public void save(ReflectedEntity ent, double simTime)
        {
            Entry e = new Entry();
            e.time = simTime;
            e.name = ent.esr.markingText;
            e.type = ent.esr.type.ToString();
            e.appearance = ent.esr.appearanceBits;
            Tools.locationVector(ent.esr, e);
            Tools.orientation(ent.esr, e);
            Tools.velocityVector(ent.esr, e);
            e.forceType = e.forceString(ent.esr.forceType);
            e.show();
            csv.WriteRecord(e);
        }
        public void close()
        {
            logFile.Flush();
            logFile.Close();
        }
    }
}
  public class Entry
    {
        static Dictionary<string, ForceType> forceMap = new Dictionary<string, ForceType> {
            { "Friendly", ForceType.FRIENDLY},
            { "Neutral", ForceType.NEUTRAL},
            { "Opposing", ForceType.OPPOSING},
            { "Other", ForceType.OTHER}};

        public double time { get; set; }
        public string type { get; set; }
        public string name { get; set; }
        public double lat { get; set; }
        public double lon { get; set; }
        public double height { get; set; }
        public double heading { get; set; }
        public double pitch { get; set; }
        public double roll { get; set; }
        public float vx { get; set; }
        public float vy { get; set; }
        public float vz { get; set; }
        public string forceType { get; set; }
        public uint appearance { get; set; }
        public double gccX { get; set; }
        public double gccY { get; set; }
        public double gccZ{ get; set; }
        public double psi { get; set; }
        public double theta { get; set; }
        public double phi { get; set; }
        public double gccVX { get; set; }
        public double gccVY { get; set; }
        public double gccVZ { get; set; }

        public string forceString(ForceType fType)
        {
            return forceMap.FirstOrDefault(x => x.Value == fType).Key;
        }
        public ForceType forceCode(string fType)
        {
            return forceMap[fType];
        }
        public void show()
        {
            Console.WriteLine("{0}: {1} - Lat: {2} Lon: {3} Alt: {4}", time, name, lat, lon, height);
        }
    }
}
JoshClose commented 7 years ago

You need to call NextRecord to end writing for that line.

DrYSG commented 7 years ago

Thanks, I notice I should also call it after the initial csv.WriteHeader();