Advertisement
Petro_zzz

Files

Dec 21st, 2023
1,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. void write_to_file(const char* filename,
  6.     const char* str) {
  7.     FILE* pfile = nullptr;
  8.     fopen_s(&pfile, filename, "a");
  9.     if (pfile != nullptr) {
  10.         fprintf(pfile,"%\n", str);
  11.         fclose(pfile);
  12.         printf("%s\n", "Job done.");
  13.     }
  14.     else {
  15.         printf("%s\n","Can not open the file.");
  16.     }
  17. }
  18.  
  19. void write_to_binfile(
  20.     const char* filename,
  21.     const int* arr,
  22.     const int size
  23. ) {
  24.     FILE* pfile = nullptr;
  25.     fopen_s(&pfile, filename, "w");
  26.     if (pfile != nullptr) {
  27.         fwrite(arr, sizeof(arr[0]), size, pfile);
  28.         //fprintf(pfile, "%\n", str);
  29.         fclose(pfile);
  30.         printf("%s\n", "Job done.");
  31.     }
  32.     else {
  33.         printf("%s\n", "Can not open the file.");
  34.     }
  35. }
  36.  
  37.  
  38. int main() {   
  39.  
  40.  
  41.     int arr[]{ 1,2,3,4,5 };
  42.     write_to_binfile("text.dat", arr, 5);
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement