C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2008, 10:45 PM   #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?
umaguptan is offline   Reply With Quote
Old 02-11-2008, 12:23 AM   #2
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 115
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
nacho4d is offline   Reply With Quote
Old 02-11-2008, 12:57 AM   #3
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 115
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
nacho4d is offline   Reply With Quote
Old 02-11-2008, 01:09 AM   #4
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,767
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'.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-11-2008, 01:30 AM   #5
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,288
No point anyway, sizeof(char) is always going to be 1.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 02-11-2008, 01:36 AM   #6
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 115
sorry for the "bite" mistake ... hahaha
__________________
Mac OS 10.6 Snow Leopard : Darwin
nacho4d is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:57 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22