zee
zee

Reputation: 188

query regarding writing in a file using c

Assume that the files are .txt

Contents of first file

hello how are you

Contents of second file

I am fine

The desired result is

hello how are you I am fine

Normally what happens is that the original contents are removed and then new contents are added in it.

I want to write in the first file in such a way that its original contents are maintained and the contents of second file are concatenated in it.

Is there any way to do that?

Upvotes: 0

Views: 36

Answers (3)

Ryan Livingston
Ryan Livingston

Reputation: 1928

It would help to know how you are trying to write the file. Likely, you are looking for the append option to FOPEN:

http://www.cplusplus.com/reference/cstdio/fopen/

FILE *f = fopen("foo.txt","a");
if (f != NULL) {
  /* Use f */
  fclose(f);
}

Upvotes: 1

Devavrata
Devavrata

Reputation: 1785

you can append another string in file by opening it in appending mode.

FILE *fp;
fp=fopen("file.txt","a");

here the next string will append after the last pointer of file.For more info link.

Upvotes: 1

Mrfence
Mrfence

Reputation: 370

Yes, you can open the file with:

fopen("fileName", "a");

This will let you append to the end if the file. More info is here: http://www.cplusplus.com/reference/cstdio/fopen/

Upvotes: 1

Related Questions