Originally Posted by
rstanley
How many of these homework assignments are you going to post?
First of all, you didn't listen to @Salem's advice in an
earlier post. If you will not take our advice, then why should we continue to respond to your posts?
As for this post:
Why are you creating a temp file at all?
Rename codeA.txt to temp.txt
rename codeB.txt to codeA.txt
rename temp.txt to codeB.txt
This could easily be done in a bash script, or a Windows batch file. No need to code this in C, unless your instructor has given you a specific assignment.
I am study for a exam and i failed on little things so i am posting a lot of that ones.
I think that if you want to "build a house" you should get good bricks and sometimes even the things are working i donīt know if the bricks are in "good shape".
I skip the argv because it was faster to code, but i understand it.
But yeah i can put the arguments inside de IDE, going to start to do that.
This need to be specific in C.
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; // argv[1]
FILE *pFileB; // argv[2]
FILE *pFileTemp;
char buffer[SIZE];
if (argc != 3) {
printf("\n- Invalid Sintax: %s Origin(file) Destination(file)\n- Ex: %s CodeA.txt CodeB.Txt\n\n", argv[0], argv[0]);
return -1;
}
// Copy codeA to a temp file
if ((pFileA = fopen(argv[1], "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(argv[1], "wt")) == NULL)
return -1;
if ((pFileB = fopen(argv[2], "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(argv[2], "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);
}