Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a CSV file in C [closed]

Tags:

c

csv

My C program generates this data. I need to place it into a CSV file, so that it can be used by another program. How can I achieve this?

Student1 Mark1 Mark2 Mark3 Mark4 Mark5
Student2 Mark1 Mark2 Mark3 Mark4 Mark5
Student3 Mark1 Mark2 Mark3 Mark4 Mark5
Student4 Mark1 Mark2 Mark3 Mark4 Mark5
Student5 Mark1 Mark2 Mark3 Mark4 Mark5
like image 686
James Mark Avatar asked Feb 16 '13 23:02

James Mark


People also ask

What special characters are not allowed in CSV?

Special characters of comma (,) quotation mark, and new line character may cause issues with a CSV import. If you get an import error, open the CSV file in Excel and use the Find & Replace tool to change or remove these specific special characters.

Why is my CSV file not opening?

CSV (comma delimited) will not open correctly and the data within will be displayed incorrectly. This is due to regional Excel settings that have default list separator options where files will either be read with a comma separator or semicolon separator.

Do commas mess up CSV files?

Some people's name use commas, for example Joe Blow, CFA. This comma breaks the CSV format, since it's interpreted as a new column. I've read up and the most common prescription seems to be replacing that character, or replacing the delimiter, with a new value (e.g. this|that|the, other ).


1 Answers

You could achieve this through fprintf

for(i = 0; i < num_of_students; i++)
     fprintf(fptr, "%s,%d,%d,%d,%d,%d\n", name, mark1, mark2, mark3, mark4, mark5);
like image 148
Ganesh Avatar answered Sep 20 '22 04:09

Ganesh