Thread: Exchange contents from one text file to other

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    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);
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    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.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by rstanley View Post
    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);
    }
    Last edited by thinkabout; 07-10-2017 at 10:20 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    > This need to be specific in C.
    rename(2): change name/location of file - Linux man page is part of standard C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by Salem View Post
    > This need to be specific in C.
    rename(2): change name/location of file - Linux man page is part of standard C.
    Code:
        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;
        }
    
    
        rename(argv[1], "temp.txt");
        rename(argv[2], argv[1]);
        rename("temp.txt", argv[2]);
        remove("temp.txt");

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    Quote Originally Posted by thinkabout View Post
    Code:
        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;
        }
    
    
        rename(argv[1], "temp.txt");
        rename(argv[2], argv[1]);
        rename("temp.txt", argv[2]);
        remove("temp.txt");
    Why do you not error check the renames to insure everything worked?
    Code:
        rename("temp.txt", argv[2]);
        remove("temp.txt");
    You renamed "temp.txt", so no need to remove a fie that no longer exists!

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by rstanley View Post
    Why do you not error check the renames to insure everything worked?
    Code:
        rename("temp.txt", argv[2]);
        remove("temp.txt");
    You renamed "temp.txt", so no need to remove a fie that no longer exists!

    Code:
        // rename - 0 on sucess , -1 on error.
        if (rename(argv[1], "temp.txt") == -1)
            return -1;
    
    
        if (rename(argv[2], argv[1]) == -1)
            return -1;
    
    
        if (rename("temp.txt", argv[2]) == -1)
            return -1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing contents of a text file
    By never_lose in forum C Programming
    Replies: 10
    Last Post: 04-28-2011, 09:25 AM
  2. pass contents of text file to 2d array
    By darksifer in forum C Programming
    Replies: 0
    Last Post: 11-22-2010, 12:45 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. storing text file contents into a string
    By m.mixon in forum C Programming
    Replies: 4
    Last Post: 07-20-2006, 11:52 AM
  5. displaying contents of a text file
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-26-2002, 02:05 PM

Tags for this Thread