Exchange contents from one text file to other
The code is working nice for me.
But feel free to give me advice.
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int main(int argc, char** argv) {
// Transfer the contents from on file to the other.
FILE *pFileA;
FILE *pFileB;
FILE *pFileTemp;
char buffer[SIZE];
// Copy codeA to a temp file
if ((pFileA = fopen("codeA.txt", "rt")) == NULL)
return -1;
if ((pFileTemp = fopen("temp.txt", "wt")) == NULL)
return -1;
while (fgets(buffer, sizeof (buffer), pFileA) != NULL)
fprintf(pFileTemp, "%s", buffer);
fclose(pFileA);
fclose(pFileTemp);
// Copy codeB to codeA
if ((pFileA = fopen("codeA.txt", "wt")) == NULL)
return -1;
if ((pFileB = fopen("codeB.txt", "rt")) == NULL)
return -1;
while (fgets(buffer, sizeof (buffer), pFileB) != NULL)
fprintf(pFileA, "%s", buffer);
fclose(pFileA);
fclose(pFileB);
// Copy tempFile to codeB
if ((pFileTemp = fopen("temp.txt", "rt")) == NULL)
return -1;
if ((pFileB = fopen("codeB.txt", "wt")) == NULL)
return -1;
while (fgets(buffer, sizeof (buffer), pFileTemp) != NULL)
fprintf(pFileB, "%s", buffer);
fclose(pFileTemp);
fclose(pFileB);
// Remove temp
remove("temp.txt");
return (EXIT_SUCCESS);
}