Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- FILE *txtFile = NULL, *binFile = NULL;
- char *buffer;
- long len;
- // OPEN FILES
- txtFile = fopen("myFile.txt", "r");
- binFile = fopen("binFile", "wb");
- if (binFile == NULL || txtFile == NULL) {
- printf("File cannot be open!\n");
- exit(1);
- }
- // OPEN FILES
- //READ DATA FROM FILE
- fseek(txtFile, 0, SEEK_END);
- len = ftell(txtFile);
- rewind(txtFile);
- buffer = calloc(1, len + 1);
- if ((fread(buffer, sizeof(char), len, txtFile)) != strlen(buffer))
- {
- printf("Errrrr!");
- }
- //READ DATA FROM FILE
- fclose(txtFile);
- printf("%s\n", buffer);
- //WRITE DATA TO BINNARY FILE
- if (fwrite(buffer, sizeof(char), strlen(buffer), binFile) != strlen(buffer)) { //same
- printf("Err!");
- }
- //WRITE DATA TO BINNARY FILE
- fclose(binFile); //close binnary file.
- ////////////////////////////////////////////
- FILE *readBinFile;
- readBinFile = fopen("binFile", "rb");
- char *buff;
- fseek(readBinFile, 0, SEEK_END);
- int binFileLen = ftell(readBinFile);
- rewind(readBinFile);
- buff = calloc(1, binFileLen + 1);
- fread(buff, sizeof(char), strlen(buffer), readBinFile);
- printf("%s\n", buff);
- fclose(readBinFile);
- free(buffer);
- free(buff);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment