68

I'm trying to append the contents of a file myfile.txt to the end of a second file myfile2.txt in c. I can copy the contents, but I can't find a way to append. Here's my code:

FILE *pFile;
FILE *pFile2;
char buffer[256];

pFile=fopen("myfile.txt", "r");
pFile2=fopen("myfile2.txt", r+);
if(pFile==NULL) {
    perror("Error opening file.");
}
else {
    while(!feof(pFile)) {
        if(fgets(buffer, 100, pFile) != NULL) {
        fseek(pFile2, -100, SEEK_END);
        fprintf(pFile2, buffer);
    }
}
fclose(pFile);
fclose(pFile2);

I don't think I'm using fseek correctly, but what I'm trying to do is call fseek to put the pointer at the end of the file, then write at the location of that pointer, instead of at the beginning of the file. Is this the right approach?

Sonofblip
  • 1,167
  • 3
  • 12
  • 20
  • 2
    (In addition to the answers below) Your `fseek` idea ought to work, but since you use `SEEK_END` the 'pointer' is already at the very end-- and *then* you go "back" 100 characters. Use `0` for the offset and it ought to work. (Minor: you check if your reading file can open, but not your writing file. Check both.) – Jongware Oct 17 '13 at 14:27
  • 2
    [why while( !feof( file ) ) is never correct](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – user3629249 May 20 '19 at 01:50
  • 1
    A typo in line 12, The curly brace is'nt closed – Hash Jul 06 '20 at 02:15

2 Answers2

102

Open with append:

pFile2 = fopen("myfile2.txt", "a");

then just write to pFile2, no need to fseek().

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • You should note however that ANSI C does require fseek() or another positioning function. – Wyatt Ward Jan 03 '16 at 17:38
  • 3
    @Wyatt8740: where? My standard says: "*Opening a file with append mode (a as the first character in the mode argument) shall cause all subsequent writes to the file to be forced to the then current end-of-file, regardless of intervening calls to fseek().* Granted that is not ANSI C, it is ISO C. – cdarke Jan 03 '16 at 22:56
  • 1
    from GNU's `man fopen`: "Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. " Looks like I read it wrong though, it's only when alternating between input and output. – Wyatt Ward Jan 04 '16 at 17:40
  • @Wyatt8740: that's fair enough, and that's my experience as well. Although I think if I was reading and writing at the same time I would probably use lower-level interfaces than stdio. – cdarke Jan 05 '16 at 10:28
  • Will using "a" append from a new line or inline? – Chaitanya Oct 05 '19 at 20:23
  • It starts inline(from the same line), obviously unless the last printed character is '\n' – Chaitanya Oct 05 '19 at 20:32
  • Line endings are not determined by the open mode (`a`) but by the way you write the characters (note that some implementations have an optional `t` for text or translate which could affect this). Most interfaces do not add line ending character/s, you have to add them yourself to the stream of bytes being written, but there are a few exceptions, like `puts` for example. You should read the doc for the writing method in use. – cdarke Oct 09 '19 at 09:43
22

Following the documentation of fopen:

``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then cur- rent end of file, irrespective of any intervening fseek(3) or similar.

So if you pFile2=fopen("myfile2.txt", "a"); the stream is positioned at the end to append automatically. just do:

FILE *pFile;
FILE *pFile2;
char buffer[256];

pFile=fopen("myfile.txt", "r");
pFile2=fopen("myfile2.txt", "a");
if(pFile==NULL) {
    perror("Error opening file.");
}
else {
    while(fgets(buffer, sizeof(buffer), pFile)) {
        fprintf(pFile2, "%s", buffer);
    }
}
fclose(pFile);
fclose(pFile2);
Sergey L.
  • 21,822
  • 5
  • 49
  • 75