#include <stdio.h>
int main() {
int a = 2;
// Step 1. open file
FILE *file = fopen("a.csv", "w");
// Step 2. record values
fprintf(file, "%d", a);
// Step 3. close file
fclose(file);
return 0;
}
fopen
function, e.g., fopen("a.csv", "w")
. w
represents Write
.fprintf
function, e.g., fprintf(file, "%d", a)
. In the above code, Because the type of the target variable is int
, use %d
. If the type is double
, use %lf
.fclose
function. e.g., fclose(file)
.double b = 3.1415;
. Use %lf
instead of %d
because %lf
is for double-type variable.,
to a CSV file.#include <stdio.h>
int main() {
int a = 2;
int b = 4;
// Step 1. open file
FILE *file = fopen("a.csv", "w");
// Step 2. record values
fprintf(file, "%d", a);
fprintf(file, ",");
fprintf(file, "%d", b);
// Step 3. close file
fclose(file);
return 0;
}
fprintf
functions, the multiple values can be saved to CSV file.fprintf(file, ",")
, a camma is saved.2, 4
is recorded by the above code.a.csv
and view it by Excel.\n
to a CSV file.#include <stdio.h>
int main() {
int a = 2;
int b = 4;
// open file
FILE *file = fopen("a.csv", "w");
// record values
fprintf(file, "%d", a);
fprintf(file, "\n"); // <- Line break
fprintf(file, "%d", b);
// close file
fclose(file);
return 0;
}
fprintf
functions, the multiple values can be saved.fprintf(file, "\n")
, the values are separeted by a line break \n
.2 \n 4
, two values arrange vertically.a.csv
and view it by Excel.#include <stdio.h>
int main() {
double array[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
// open file
FILE *file = fopen("a.csv", "w");
// Recording
fprintf(file, "%lf", array[0]);
fprintf(file, "\n");
fprintf(file, "%lf", array[1]);
fprintf(file, "\n");
fprintf(file, "%lf", array[2]);
fprintf(file, "\n");
fprintf(file, "%lf", array[3]);
fprintf(file, "\n");
fprintf(file, "%lf", array[4]);
// close file
fclose(file);
return 0;
}
Of course, the following code is more better.
for(int i = 0; i < 5; i++){
fprintf(file, "%lf", array[i]);
if(i < 4){ // Final "\n" is not necessary.
fprintf(file, "\n");
}
}
For
loop statement, we can save values of array.a.csv
and view it by Excel.#include <stdio.h>
#define MAX_SIZE 5
// User-defined function for making CSV
// Input: File name, array data
// Result: Save file
void make_csv(char *filename, double *arr){
// open file
FILE *file = fopen(filename, "w");
// record values
for (int i = 0; i < MAX_SIZE; i++) {
fprintf(file, "%lf", arr[i]);
if(i < MAX_SIZE - 1){ // Final "\n" is not necessary.
fprintf(file, "\n");
}
}
// close file
fclose(file);
}
// main function
int main() {
double array[MAX_SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};
make_csv("var.csv", array); // <- use it here.
return 0;
}
make_csv(char *filename, double *arr)
.*
symbol, e.g., char *filename
.arr
is the double-type array, need *
, e.g., double *arr
.#include <stdio.h>
#include <math.h>
#define MAX_SIZE 1000
// User-defined function for making CSV
// Input: File name, array data
// Result: Save file
void make_csv(char *filename, double *arr){
// open file
FILE *file = fopen(filename, "w");
// record values
for (int i = 0; i < MAX_SIZE; i++) {
fprintf(file, "%lf", arr[i]);
if(i < MAX_SIZE - 1){ // Final "\n" is not necessary.
fprintf(file, "\n");
}
}
// close file
fclose(file);
}
int main() {
double input_x[MAX_SIZE];
double output_y[MAX_SIZE];
// Making input domain
// i = 0 to 999
// 0.0, 0.1, ..., 99.9
for(int i = 0; i < MAX_SIZE; i++){
input_x[i] = 0.1 * i;
}
// Get value of sin function
for(int i = 0; i < MAX_SIZE; i++){
output_y[i] = sin(input_x[i]);
}
// save
make_csv("sin_function.csv", output_y);
return 0;
}
0.0, 0.1, ..., 99.9
are assigned to input_x
.math.h
.output_y
by output_y[i] = sin(input_x[i]);
.output_y
by make_csv
function.sin_function.csv
, open it via Excel, and making graph.int
-type variables and file name from keyboard by using scanf
, and save these values separeted by comma. Submit screenshots of your code and display on Excel.double
-type variables and file name from keyboard by using scanf
, and save these values separeted by line break \n. Submit screenshots of your code and display on Excel.output_y[i] = sin(input_x[i]) + 0.1*input_x[i]
. After that, save it and make a graph via Excel. Submit screenshot of the graph.math.h
and make graph. Submit screenshot of the graph and Your equation. See: https://www.tutorialspoint.com/c_standard_library/math_h.htm