Zig
Zig
Create CSV File
See more CSV Examples
Demonstrates how to create a new CSV file with some simple content.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This sample code creates a new CSV file (sample.csv)
// that contains this content:
//
// year,color,country,food
// 2001,red,France,cheese
// 2005,blue,"United States",hamburger
const csv = try chilkat.Csv.init();
defer csv.deinit();
// Indicate that the 1st row
// should be treated as column names:
csv.setHasColumnNames(true);
try csv.setColumnName(0, "year");
try csv.setColumnName(1, "color");
try csv.setColumnName(2, "country");
try csv.setColumnName(3, "food");
try csv.setCell(0, 0, "2001");
try csv.setCell(0, 1, "red");
try csv.setCell(0, 2, "France");
try csv.setCell(0, 3, "cheese");
try csv.setCell(1, 0, "2005");
try csv.setCell(1, 1, "blue");
try csv.setCell(1, 2, "United States");
try csv.setCell(1, 3, "hamburger");
// Write the CSV to a string and display:
const csv_doc = try csv.saveToString(alloc);
std.debug.print("{s}\n", .{csv_doc});
// Save the CSV to a file:
csv.saveFile("out.csv") catch {
std.debug.print("{s}\n", .{try csv.getLastErrorText(alloc)});
};
}