C File Processing

logistic_guy

Full Member
Joined
Apr 17, 2024
Messages
789
Write a program that uses the sizeof operator to determine the sizes in bytes of the various data types on your computer system. Write the results to the file "datasize.dat" so you may print the results later. The format for the results in the file should be as follows (the type sizes on your computer might be different from those shown in the sample output):

c_program.png
 
Write a program that uses the sizeof operator to determine the sizes in bytes of the various data types on your computer system. Write the results to the file "datasize.dat" so you may print the results later. The format for the results in the file should be as follows (the type sizes on your computer might be different from those shown in the sample output):

View attachment 39055
show us your effort/s to solve this problem.
 
At the beginning, I will try to write a simple program. Then, I will develop it to meet the requirement. For now, I will use the following:

\(\displaystyle \bold{1.}\) FILE
\(\displaystyle \bold{2.}\) fprintf
\(\displaystyle \bold{3.}\) sizeof
\(\displaystyle \bold{4.}\) fopen
\(\displaystyle \bold{5.}\) fclose

FILE is a structure data type in C, so linking it with a pointer will give me the flexibility of file handling.

When FILE is linked with a pointer it will look like FILE*.

FILE* is called a pointer to a FILE structure and by using it I can perform various operations on files, such as opening, reading, writing, and closing them. I will name it cat🐱

FILE* cat;

Now I want to create a file and write inside it. I will call my file \(\displaystyle \text{kitten}\) and will choose its type to be \(\displaystyle \text{txt}\). That's the job of my cat.

FILE* cat = fopen("kitten.txt", "w") ;

My full first trial program is:
---------------------------
#include <stdio.h>

int main() {

FILE* cat = fopen("kitten.txt", "w") ;

fprintf(cat, "%s", "Data type") ;
fprintf(cat, "%s\n", "Size") ;

fprintf(cat, "%s", "long double") ;
fprintf(cat, "%4d\n", sizeof(long double)) ;

fclose(cat) ;

return 0 ;
}
---------------------------
cat.png


 
🤠

I improved the formatting of my program and I also added a new data type double.

Here is the code so far with a screenshot:

#include <stdio.h>

int main() {

FILE* cat = fopen("kitten.txt", "w") ;

fprintf(cat, "%-11s", "Data type") ;
fprintf(cat, "%7s\n", "Size") ;

fprintf(cat, "%-11s", "double") ;
fprintf(cat, "%7d\n", sizeof(double)) ;

fprintf(cat, "%-11s", "long double") ;
fprintf(cat, "%7d\n", sizeof(long double)) ;

fclose(cat) ;

return 0 ;
}

file.png

I am expecting my code to be completely ready in the next post.

💪😾
 
I edited the formatting of the text again to suit all types of data together.

Note: I chose the file to be "kitten.text" instead of "datasize.dat" because I cannot open the latter in my computer to show you the main purpose of the program.

And here is my final code with a screenshot.

🧑‍🍳

#include <stdio.h>

int main() {

FILE* cat = fopen("kitten.txt", "w") ;

fprintf(cat, "%-18s", "Data type") ;
fprintf(cat, "%7s\n", "Size") ;

fprintf(cat, "%-18s", "char") ;
fprintf(cat, "%7d\n", sizeof(char)) ;

fprintf(cat, "%-18s", "unsigned char") ;
fprintf(cat, "%7d\n", sizeof(unsigned char)) ;

fprintf(cat, "%-18s", "short int") ;
fprintf(cat, "%7d\n", sizeof(short int)) ;

fprintf(cat, "%-18s", "unsigned short int") ;
fprintf(cat, "%7d\n", sizeof(unsigned short int)) ;

fprintf(cat, "%-18s", "int") ;
fprintf(cat, "%7d\n", sizeof(int)) ;

fprintf(cat, "%-18s", "unsigned int") ;
fprintf(cat, "%7d\n", sizeof(unsigned int)) ;

fprintf(cat, "%-18s", "long int") ;
fprintf(cat, "%7d\n", sizeof(long int)) ;

fprintf(cat, "%-18s", "unsigned long int") ;
fprintf(cat, "%7d\n", sizeof(unsigned long int)) ;

fprintf(cat, "%-18s", "float") ;
fprintf(cat, "%7d\n", sizeof(float)) ;

fprintf(cat, "%-18s", "double") ;
fprintf(cat, "%7d\n", sizeof(double)) ;

fprintf(cat, "%-18s", "long double") ;
fprintf(cat, "%7d\n", sizeof(long double)) ;

fclose(cat) ;

return 0 ;
}

file2.png
 
Top