![]() |
| | #1 |
| Registered User Join Date: Feb 2008
Posts: 1
| 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 | |
| | #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 | |
| | #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 | |
| | #4 | ||
| Mysterious C++ User 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:
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:
| ||
| Elysia is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |