Details of dll dynamic library development and call and file read and write small procedures

  • 2020-05-30 20:51:49
  • OfStack

Detail dll dynamic library development and call and file read and write small procedures

First let's learn 1 under dynamic library calls, first find the dynamic library. dll and. lib file and import it into the homologous file under the same level of the folder, and then added to the header files, and right-click on the project, and then click on the link, link to our lib file (1 set if full name including extension), and then we can call the dynamic library function.

Dll is our specific function, lib is our function description file.


#include <stdio.h> 
#include <stdlib.h> 
/* 
   This code is the use of file read and write operations  
*/ 
#pragma warning(disable:4996) 
#define MAX_DATA_LEN 4096 
// The encryption function  
int FileSymEnc(char * from, char * to); 
 
void main() { 
 
  char * from = "C:/Users/Administrator/Desktop/ Copy of structures .docx"; 
  char * to = "C:/Users/Administrator/Desktop/ Copy problems with replica structures .docx"; 
  // The following function is called to read and write the file  
  int re = FileSymEnc(from, to); 
  if (re == 0) { 
    printf(" File read/write error! "); 
  } 
  system("pause"); 
} 
 
int FileSymEnc(char * from, char * to) { 
  // Returns a value to determine the execution status ,-0 On behalf of the failure ,1 On behalf of success  
  int re = 1; 
  // Write a few  
  int writtenLen = 0; 
  // I'm going to write a few  
  int plainlen = 0; 
  // Allocation in the heap area 4k Memory space  
  unsigned char * buff = malloc(MAX_DATA_LEN); 
  buff = memset(buff, 0, MAX_DATA_LEN); 
  FILE * f = fopen(from, "rb"); 
  FILE * t = fopen(to, "wb"); 
  if (f == NULL) { 
    printf(" Error opening read file! "); 
    goto END; 
  } 
  if (t == NULL) { 
    printf(" Open write file error! "); 
    goto END; 
  } 
 
  while (!feof(f)) 
  { 
 
    writtenLen = fread(buff, 1, MAX_DATA_LEN, f); 
    // Read whether the end of the file has been read, and if so, jump out of the loop  
    if (feof(f)) 
    { 
      plainlen = writtenLen; 
      break; 
    } 
 
    if (writtenLen != MAX_DATA_LEN) { 
      // Failed to read  
      printf(" File reading failed! "); 
      re = 0; 
      goto END; 
    } 
    // Read the file successfully  
    writtenLen = fwrite(buff, 1, MAX_DATA_LEN, t); 
    if (writtenLen != MAX_DATA_LEN) { 
      printf(" File write failed! "); 
      re = 0; 
      goto END; 
    } 
  } 
 
  // It can't be handled outside 4K We don't have to do extra small data reads and writes, but it has something to do with the way we encrypt  
  writtenLen = fwrite(buff, 1, plainlen, t); 
  if (writtenLen != plainlen) { 
    printf(" File write failed! "); 
    re = 0; 
    goto END; 
  } 
 
END: 
  // Heap allocated memory is freed  
  if (buff != NULL) { 
    free(buff); 
    buff = NULL; 
  } 
  // Close the file  
 
  if (f != NULL) { 
    fclose(f); 
    f = NULL; 
  } 
  if (t != NULL) { 
    fclose(t); 
    f = NULL; 
  } 
  return re; 
} 

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: