Thread: Inserting text into a file

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    34

    Inserting text into a file

    Hi all,

    I'm having difficulties inserting a string into a text file. Here's my code:

    Code:
    int main() {
    
    	FILE *f;
    	char c=0;
    	int br=0;
    
    	if ((f=fopen("fajl.htm", "r+"))==NULL) {
    		exit(1);
    	}
    
    	while ((c=fgetc(f))!=EOF) {
    
    		if (c=='&') {
    				fputs("blah", f);
    				printf("Job done.\n");
    		}
    	}
    
    	fclose(f);
    
    	return 0;
    
    }
    What I am trying to do is insert a string after a certain character. This code does not work for some reason, when it goes through the file and finds this specific character, it will not insert my string, but continue to the next character.

    What am I doing wrong?

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    you're trying to write to the file right? fix the mode then..

    besides , the while loop would execute anyway regardless if the file was successfuly opened or not.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by Brain Cell
    you're trying to write to the file right? fix the mode then..
    If I use the w mode, it will delete the contents of the file before writing.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can use the a mode to append.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Guys, "r+" is the correct mode for him. "a" would stick him at the end of the file. He wants to insert after a certain character in the file. And how would the while() loop execute if the file is incorrectly opened if exit() is being called in that situation?

    It would be helpful to see the contents of the file.

    NOTE: You can't "insert" text in a file. You'll overwrite what's already there. You can read in the rest of the file after that point to a buffer, write your new text, and then dump the buffer back onto the end of the file though.
    Last edited by itsme86; 11-05-2004 at 02:43 PM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by itsme86
    NOTE: You can't "insert" text in a file. You'll overwrite what's already there. You can read in the rest of the file after that point to a buffer, write your new text, and then dump the buffer back onto the end of the file though.
    What I'm actually trying to do is replace ampersands (&) in a HTML file with &. So when the ampersand is found, the program would insert amp; after it.

    What you're suggesting is to define a large character array where I'd put what remains in the file after the specific character? How large should the array be, considering that the HTML files vary from 5 to 80 KB?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's one option. Another option would be to use a separate output file. You could read each character in the file like you are now. Write the character to the output file and then check if it was an ampersand. If it was, also write "amp" to the output file.

    To find out the size of the file (in case you go with the first option) you can use stat() on the file. The st_size member is the size of the file. Then you can malloc() that many bytes for the buffer.
    If you understand what you're doing, you're not learning anything.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (c = fgetc( infile )) != EOF )
    {
        if( c == '&' )
        {
            fprintf( outfile, "&" );
        }
        else
        {
            fputc( c, outfile );
        }
    }
    You did say "every &".

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by itsme86
    That's one option. Another option would be to use a separate output file. You could read each character in the file like you are now. Write the character to the output file and then check if it was an ampersand. If it was, also write "amp" to the output file.

    To find out the size of the file (in case you go with the first option) you can use stat() on the file. The st_size member is the size of the file. Then you can malloc() that many bytes for the buffer.
    Thanks, you've been much helpful!!

  11. #11
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Guys, "r+" is the correct mode for him. "a" would stick him at the end of the file.
    aw i didn't notice the plus nor that you actually want to write AND read from the file.

    sorry Ariod..
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  12. #12
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by itsme86
    That's one option. Another option would be to use a separate output file. You could read each character in the file like you are now. Write the character to the output file and then check if it was an ampersand. If it was, also write "amp" to the output file.

    To find out the size of the file (in case you go with the first option) you can use stat() on the file. The st_size member is the size of the file. Then you can malloc() that many bytes for the buffer.
    The idea is a good one, however this is assuming the op is on unix or unix variant.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by caroundw5h
    The idea is a good one, however this is assuming the op is on unix or unix variant.
    True enough. I failed to mention that. Thanks.
    If you understand what you're doing, you're not learning anything.

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. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM