Thread: Doubt in concatenating contents of two text files in C program

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Question Doubt in concatenating contents of two text files in C program

    Hi i wrote the following program for concatenating two files.

    But if the contents are as follows:

    a.txt:
    hi

    b.txt:
    all

    My program concatenates like this:

    hi
    all

    But i need it like

    hi all

    /*
    * Filename :concatenateInFirstFile.c
    * Author :Uma S G
    * Description :Concatenate given two files in First File
    */
    #include <stdio.h>
    /* MACRO DEFINITIONS */
    #define MAX_LEN 1024
    #define NAME_SIZE 15

    /*
    * Function :main
    * Description :First entry point to program
    * Arguments :None
    *
    */
    int
    main() {
    /* variable declaration */
    char acBuff[MAX_LEN];
    char acFirstFile[NAME_SIZE], acSecondFile[NAME_SIZE];
    FILE *fp1;
    FILE *fp2;

    /* Getting files to be conctenated */

    printf("\n Enter the First File Name :\t");
    gets(acFirstFile);
    printf("\n Enter The second File Name :\t");
    gets(acSecondFile);

    /* opening file for reading */
    fp2 = fopen(acSecondFile, "r");
    if (fp2 == NULL) {
    printf("\n Unable to open the file %s for reading!!...", acSecondFile);
    return (1);
    }
    while (fgets(acBuff, MAX_LEN, fp2) != NULL) {
    /* opening file for appending */
    fp1 = fopen(acFirstFile, "a");
    if (fp1 == NULL) {
    printf("\n Unable to open the file %s for reading!!...", acFirstFile);
    return (1);
    }

    fputs(acBuff, fp1);
    }
    /* closing files */
    fclose(fp1);
    fclose(fp2);
    return (0);
    }


    Please help me with this?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    i might be wrong but, ...
    first : it is not necessary to open acFirstFile every time you want to write it, it should be enough to open it before the while loop
    once you open it, make you deleted the new line character "\n" before start appending

    and please intend your source , it will make it easier to read
    Mac OS 10.6 Snow Leopard : Darwin

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    I compiled your source and I have to tell you... the problem you are trying to solve is because your file a.txt should have a "\n" new line character before the EOF . so, in that case you can use fseek() to move inside the file (move one 1 bite from the end of file in backwards direction if the last character is "\n")
    I am not sure if it is 1 bite but you can use something like sizeof('/n');
    Mac OS 10.6 Snow Leopard : Darwin

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to use code tags for code.
    What you should do is read the contents of file 1 using fgets, strip the newline at the end of the buffer and write it to the file. Then read the contents of the second file and write it to the file and you're done.
    And don't ever use gets. Always use fgets. That includes reading input from the user.

    Quote Originally Posted by nacho4d View Post
    I am not sure if it is 1 bite but you can use something like sizeof('/n');
    Btw, "bite" is an entirely different word! It's byte.
    And sizeof('/n') won't work. '/n' != '\n'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No point anyway, sizeof(char) is always going to be 1.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    sorry for the "bite" mistake ... hahaha
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'd like to load a text file's contents cleanly at compile time
    By CharlieLewaska in forum C Programming
    Replies: 3
    Last Post: 01-31-2006, 11:10 AM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM