0

Recently, I've started working on File Operations in C, and I was successfully able to copy the contents of one file to another.

I went a step ahead to read the content of a file from the start of the file and then to write the same content at the end of the same file. (Similar to appending). But I'm not able to do so.

Here is the code that I've written. Can anyone suggest, what is the mistake I've done here?

#include <stdio.h> int main(int argc, char **argv) { int size = 0, index = 0; char byte = 0; FILE *fp_read, *fp_write; if ((fp_read = fopen(argv[1], "r+")) == NULL) { printf("Unable to Open File %s\n", argv[1]); } fp_write = fp_read; fseek(fp_write, 0, SEEK_END); size = ftell(fp_write); printf("The Size of the file %s:\t%d\n", argv[1], size); for (index = 0; index < size; index++) { if (fread(&byte, 1, 1, fp_read) != 1) { printf("Unable to Read from the file at Location:\t%d\n", index); } if (fwrite(&byte, 1, 1, fp_write) != 1) { printf("Unable to Write to the file at Location:\t%d\n", index); } } if (fclose(fp_read)) { printf("Unsuccessful in Closed the File\n"); } return 0; } 

Regards,

Naruto Uzumaki

4
  • You should tag the language (and not put it in the title). It helps people find it, and also changes how the syntax highligting of code works. Commented Jul 15, 2015 at 18:06
  • 1
    Just use one file pointer. fseek back to the beginning of the file before you read it. Then fseek back to the end to write it (although the file pointer is at the end, the fseek may be needed). EDIT: from c99 standard "[for update mode] output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file" Commented Jul 15, 2015 at 18:12
  • At the statement ftell(), the 'file pointer' is at the end of the file. So a fread()will be trying to read past the end of the file, so naturally, the fread() fails, returning EOF. Copying the file descriptor pointer does not create a second access to the file, it only makes a copy of the pointer. you could just call fopen with mode "a" to set the fp_write pointer. Commented Jul 16, 2015 at 1:12
  • when expecting a command line parameter (in this case, a file name) always first, check argc to assure that such a parameter exists. if it does not exist, then output a 'usage' message and exit the program. Commented Jul 16, 2015 at 1:13

2 Answers 2

2

Your mistake is treating fp_read and fp_write as if they weren't pointing to the same FILE object.

Sign up to request clarification or add additional context in comments.

2 Comments

@Siguza: That makes them point to the same FILE object, as I said. This means, for example, fseek(fp_write, 0, SEEK_END); has the same effect as fseek(fp_read, 0, SEEK_END);.
Yes, you are right. Both the file pointers are pointing to the End of file.
1

Similar to the answer prior, you are treating both pointers as if they are referencing different FILE streams. When you perform the fseek function, you are changing where the FILE pointer is located. As a check, try running ftell on both fp_read and fp_write and printing them out to determine if they are at different spots.

Edit: As mentioned by the comment, you could open a second stream for the fp_write variable as follows to append.

fp_write = fopen("somefile", "a"); 

1 Comment

I would say "different streams" instead of "different files" since it would be possible to open the file twice and have two different streams accessing the same file, in which case the technique may work. Not sure it that's a good idea, of course!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.