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?
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.
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.
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) isThis 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.




