cs:c_language:write_and_read_a_binary_file
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/write_and_read_a_binary_file?do=diff&rev2%5B0%5D=1551188114&rev2%5B1%5D=1551188114&difftype=sidebyside
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:write_and_read_a_binary_file [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Write and read a binary file (fwrite and fread) ====== | ||
+ | **Concepts: | ||
+ | Functions '' | ||
+ | |||
+ | **Text:**\\ | ||
+ | After the writing of a vector of structures (the elements of the structure are a vector of characters containing the name of a student, a variable of type '' | ||
+ | * open in write mode a file with name "'' | ||
+ | * written the file in a binary way by using the function '' | ||
+ | * close the file | ||
+ | * reopen the file for reading it | ||
+ | * read the file by using the function '' | ||
+ | * print all the data contained in the vector of structures | ||
+ | * close the file | ||
+ | |||
+ | **Video solution: | ||
+ | Video with the explanation of the solution: | ||
+ | |||
+ | < | ||
+ | <iframe width=" | ||
+ | </ | ||
+ | |||
+ | **Solution: | ||
+ | <file C binary_file_fwrite_fread.c> | ||
+ | /* Write and read a binary file (fwrite and fread) */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define LEN 50 | ||
+ | #define N_STUD 2 | ||
+ | |||
+ | typedef struct stud{ | ||
+ | char name[LEN+1]; | ||
+ | unsigned int id; | ||
+ | float average; | ||
+ | }stud_t; | ||
+ | |||
+ | |||
+ | int main() { | ||
+ | FILE *fp; | ||
+ | stud_t student[N_STUD]; | ||
+ | int i; | ||
+ | char row[LEN+1]; | ||
+ | unsigned int n_stud; | ||
+ | |||
+ | strcpy(student[0].name, | ||
+ | student[0].id = 120000; | ||
+ | student[0].average = 25.5; | ||
+ | |||
+ | strcpy(student[1].name, | ||
+ | student[1].id = 120001; | ||
+ | student[1].average = 28.5; | ||
+ | |||
+ | fp = fopen(" | ||
+ | if (fp == NULL){ | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | |||
+ | /* Write the file */ | ||
+ | /* NOTE 2: a binary file sometimes cannot be readable | ||
+ | | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | /* Write in binary all the data contained in the structure */ | ||
+ | fwrite(student, | ||
+ | |||
+ | /* DIMENSION OF THE GENERATED FILE | ||
+ | The dimension of the generated file will be: | ||
+ | | ||
+ | in the case of the computer used to test the program: | ||
+ | 2*60 = 120 byte | ||
+ | */ | ||
+ | | ||
+ | fclose(fp); /* Close the file */ | ||
+ | |||
+ | |||
+ | | ||
+ | fp = fopen(" | ||
+ | if (fp == NULL){ | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* Read the file */ | ||
+ | n_stud = 0; | ||
+ | while( fread(& | ||
+ | n_stud++; | ||
+ | } | ||
+ | |||
+ | fclose(fp); /* Close the file */ | ||
+ | |||
+ | | ||
+ | /* Print the read records */ | ||
+ | for (i=0; i< | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/write_and_read_a_binary_file?do=diff&rev2%5B0%5D=1551188114&rev2%5B1%5D=1551188114&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/write_and_read_a_binary_file.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1