Thread: appending text to existing text file

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    appending text to existing text file

    Hi team,
    This is doing my head in...and I'm sure i'm SO close to solving this! I need some guided help on how I can modify my code below, so that I can append some additional text to the beginning of a text file, which already has content within it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char str[80] = "This is just some random text.\n";
      FILE *fp;
      char *p;
      int i;
    
      /* open file3.txt for output */
      if((fp = fopen("file3.txt", "w"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
      }
    
      /* write str to disk */
      p = str;
      while(*p) {
        if(fputc(*p, fp)==EOF) {
          printf("Error writing file.\n");
          exit(1);
        }
        p++;
      }
      fclose(fp);
    
      /* open file3.txt for input */
      if((fp = fopen("file3.txt", "r"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
      }
    
      /* read back the file */
      for(;;) {
         i = fgetc(fp);
         if(i == EOF) break;
         putchar(i);
      }
      fclose(fp);
    
      return 0;
    }
    Basically, what this overwrites the contents of the file and just adds "This is just some random text."...

    Would REALLY welcome any of you to show me the proper code!

    Cheers,
    K

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    According to http://www.cplusplus.com/reference/c...dio/fopen.html you need to use the mode "a" to append to the file.

    EDIT: U want to add to front. sorry. read the reply below me
    Last edited by 39ster; 05-28-2008 at 08:04 AM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    To add text to the front, you have two options:

    1) Write to a new file, add the new text, and then copy all the text from the source file to the new file and when done, delete the original source file and rename the new file to the old name.

    2) Open the file for read and write, read the whole file into memory, reset the file position to zero, and then write your new data followed by all the data you read into memory.

    So, therefore, you are not so close.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    4
    thanks guys for your quick reply!
    back to the drawing board I go.

    Todd/39ster - do you have any references to how to do the above mentioned?

    Cheers,

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    kpax, I think it would be a very trivial task to open one file, read, then write into another file and repeat until you've finished writing the entire contents.
    Try doing that and I'm sure you can finish your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Which one are you wanting to do? Have you learned about allocating memory yet?
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    4
    thanks Elysia.

    Nope haven't got to that stage about memory allocation.

    I understand exactly what I need to do - the hard parts actually coding it

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    It's only hard when you let the whole scope of the effort overwhelm you.

    Divide and conquer.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kpax View Post
    thanks Elysia.

    Nope haven't got to that stage about memory allocation.

    I understand exactly what I need to do - the hard parts actually coding it
    And you will not learn how to do it if you don't do it yourself - just like you can't learn how to drive a car by watching someone drive on telly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM