Basic C programming 7: Output File¶

Yuto Omae @ Nihon University, Japan¶

  • We use online compiler and debugger for c/c++.
  • https://www.onlinegdb.com/online_c_compiler
  • It is important to record analytical results by using C programming, e.g., win-lose results of games, user information, and so on.
  • Here is how to record variables as output CSV file by C programming.

Code 1: Most simple code for output data¶

  • Here is the code for saving the one variable to a CSV file.
#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;
}

Key points:¶

  • The saving data is conducted by following three steps.
  • Step1. Open File: Give a file name via fopen function, e.g., fopen("a.csv", "w"). w represents Write.
  • Step2. Record: Give a variable that you want to save via 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.
  • Step3. Close File: Close the file via fclose function. e.g., fclose(file).

Works:¶

  • Save double b = 3.1415;. Use %lf instead of %d because %lf is for double-type variable.

Code 2: Recording multiple data separated by comma (,)¶

  • Here is the code for recording two variable separated by comma , 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;
}

Key points¶

  • By arranging multiple fprintf functions, the multiple values can be saved to CSV file.
  • By fprintf(file, ","), a camma is saved.
  • 2, 4 is recorded by the above code.

Works¶

  • Download a.csv and view it by Excel.

Code 3: Recording multiple data separated by line break (\n)¶

  • Here is the code for saving multiple variables separated by line break \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;
}

Key points¶

  • By arranging fprintf functions, the multiple values can be saved.
  • In the above code, by fprintf(file, "\n"), the values are separeted by a line break \n.
  • Because the result is 2 \n 4, two values arrange vertically.

Works¶

  • Download a.csv and view it by Excel.

Code 4: Recording Array variable¶

  • Here is the code for saving array-values by line break.
#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");
    }
}

Key points¶

  • By using For loop statement, we can save values of array.

Works¶

  • Download a.csv and view it by Excel.
  • Make the code for recording array values separated by comma (,) and check result.

Code 5: User-defined function for recording array values¶

  • Here is the user-defined function for saving array-values.
#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;
}

Key points¶

  • See make_csv(char *filename, double *arr).
  • Because the variable for file name consists of multi characters, need * symbol, e.g., char *filename.
  • Because arr is the double-type array, need *, e.g., double *arr.

Code 6: Sin function¶

  • Here is the code for saving values of $f(x) = \sin(x)$ to the CSV file.
#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;
}

Key points¶

  • By the above code, 0.0, 0.1, ..., 99.9 are assigned to input_x.
  • Mathematical functions can be used by loading the header file math.h.
  • Assign the Sine values into the array output_y by output_y[i] = sin(input_x[i]);.
  • Save the Sine values output_y by make_csv function.

Works¶

  • Download sin_function.csv, open it via Excel, and making graph.

Assignments¶

  • (1) Make the code that a user input five values of 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.
  • (2) Make the code that a user input five values of 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.
  • (3) On the code 6, change mathematical function from $\sin(x)$ to $\sin(x) + 0.1 x$, that is, 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.
  • (4) Use any function in math.h and make graph. Submit screenshot of the graph and Your equation. See: https://www.tutorialspoint.com/c_standard_library/math_h.htm