Append to the end of a file in C
Asked Answered
C

2

70

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?

Cop answered 17/10, 2013 at 14:13 Comment(3)
(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.)Starinsky
why while( !feof( file ) ) is never correctLymphangitis
A typo in line 12, The curly brace is'nt closedMash
X
105

Open with append:

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

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

Xray answered 17/10, 2013 at 14:16 Comment(7)
You should note however that ANSI C does require fseek() or another positioning function.Brinkema
@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.Xray
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.Brinkema
@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.Xray
Will using "a" append from a new line or inline?Drover
It starts inline(from the same line), obviously unless the last printed character is '\n'Drover
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.Xray
V
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);
Variation answered 17/10, 2013 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.