learn through suffering C language learn through suffering 
C language

Practice Problem 17

Basics


Question 1-1
What do you call a file that contains only textual information?


Question 1-2
What is the specific name given to files containing only numerical values
to distinguish them from the files mentioned above?

Program reading

When this program is executed, what is the name of the file that will be written to, in what format, and with what values?

Question 2-1
#include <stdio.h>

int main(void)
{
    FILE* fp;
    int dat[3] = { 10, 20, 30 };

    fp = fopen("file.dat", "wb");
    fwrite(&dat, sizeof(dat), 1, fp);
    fclose(fp);

    return 0;
}

Program Manual


Number name Average test score
1 James 0
2 Olivia 90
3 Michael 40
4 Thomas 7


Question 3-1
Please create a program that generates the above table in a format that can be opened in Excel.
Hint: CSV files can be opened with any spreadsheet software.


explanatory


Question 4-1
Explain concisely why information that should simply be displayed on the screen is deliberately saved as a file.

Fundamentals (Answer Key)


Solution 1-1
text file


Solution 1-2
Binary file

Program Reading (Solution Example)


Solution 2-1
The values 10, 20, and 30 are written in binary format to the file.dat file.

Program Documentation (Example Solution)


Solution 3-1
#include <stdio.h>

int main(void)
{
    FILE* fp;

    fp = fopen("test.csv", "w");

    fprintf(fp, "Number,name,Average test score\n");
    fprintf(fp, "1,James,0\n");
    fprintf(fp, "2,Olivia,90\n");
    fprintf(fp, "3,Michael,40\n");
    fprintf(fp, "4,Thomas,70,\n");

    fclose(fp);

    return 0;
}

While we're using string literals here, it would be even better if you were using arrays or structs.
Points will be deducted if you forget to use the fclose function.

Descriptive (answer example)


Solution 4-1
Information displayed on the screen disappears quickly, but
saving it to a file allows for semi-permanent storage and makes editing and sharing much easier.



About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...