Write To a File Without Deleting

This is a discussion on Write To a File Without Deleting within the C Programming forums, part of the General Programming Boards category; Ok...so I'm trying to write a simple program to write the letter "a" into a text file. The problem is, ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    Write To a File Without Deleting

    Ok...so I'm trying to write a simple program to write the letter "a" into a text file. The problem is, is that when I already have things written in the text file, and run the program, it deletes all the other stuff in the file, and puts "a"...I have no idea how I can put the letter "a" after all the other stuff(whatever that may be) in the file.

    I'd also like to learn how to find certain text in a file, then based on where that is, place some data after the text that was found...I hope I explained that well.

    So if someone could lead me in the right direction, I'd really appreciate it. I've learned to do the following code from a book, but it didn't teach me how to do either of the things above...And I am trying to think of a way to do it, but can't come up with one. Nor has google helped(might just be searching for the wrong sorta thing).

    Code:
    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
    	FILE *filePtr; 
    	char fileContents; 
    	char c;
    	
    	c = 'a';
    	
    	filePtr = fopen("../../test.txt", "r"); 
    	
    	if(filePtr == NULL) 	{
    		printf("Error opening file.");
    	}
    	else 
    	{
    		while((fileContents = fgetc(filePtr)) != EOF) 
    		{
    			putchar(fileContents); 
    			
    		}
    		fclose(filePtr); 
    	}
    	
    	filePtr = fopen("../../test.txt", "w");
    	
    	if(filePtr == NULL)
    	{
    		printf("Error opening file, to write to it.");
    	}
    	else 
    	{
    		fputc(c, filePtr);
    	}
    
    	
        return 0;
    }

  2. #2
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    Change filePtr = fopen("../../test.txt", "w"); to filePtr = fopen("../../test.txt", "a"); for 'append' mode.

    Remember, Google is your friend.
    - Leeor

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by leeor_net View Post
    Change filePtr = fopen("../../test.txt", "w"); to filePtr = fopen("../../test.txt", "a"); for 'append' mode.

    Remember, Google is your friend.
    Thank you, I had actually just found that in my book when I saw your post. I somehow missed that part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21