Thread: file problem

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    file problem

    i'm trying to open a file and read all the charecters and remove all punctuation then write back to the file , however for words like that's i dont want to remove the ' . for example:

    bin.txt

    Code:
     'the house ! ? house? when sat on that's ,,.
    should end up as :

    Code:
      the house     house  when sat on that's
    i want to insert a blank space where the punctuation was. i've attempted to do this however my code doesnt seem to be changing the file, here is my code

    Code:
       
    
    
    void append( void )
    {
      
     	 FILE *f;
     	 int i;
       	 char temp[80];
       	 char finish[80];
       	 
    
       	 if ( ( f = fopen(NAME, "r+b" ) ) == NULL) {
    		exit(1);
         }
    
         while ( fgets ( temp, sizeof temp, f ) != NULL ) {
        	   if( temp[79] == '\n' ){
                   temp[80] == '0';
               }
         
         
         for( i = 0; i < 80; i++){
             
             if ( ( temp[i] == '.' || temp[i] == '!' || temp[i] == ',' || 
                    temp[i] == '-' || temp[i] == '?' || temp[i] == '+' || 
                    temp[i] == '"' || temp[i] == ':' || temp[i] == ';' || 
                    temp[i] == '`' || temp[i] == '\'' && temp[i+1] == ' ' ||
                    temp[i] == '(' ||  temp[i] == ')' ) ){
                    temp[i] = ' ';
             } 
                
         finish[i] = temp[i];
                
         
         fseek( f, -1, SEEK_CUR ); 
         fputc( finish[i], f );
         fflush( f ); 
         }         
         
         }           
         fclose ( f );
    
    }
    where have i gone wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well your indentation could be better for starters. It might look OK in your editor which can intelligently interpret mixes of spaces and tabs, but the result is a mess when posted on the board. Change the editor to only indent using spaces.

    Second, trying to update a text file in-place is not really do-able. It's much easier to simply read in the string from the old file, and write out the string to the new file.

    Third, your seek only seeks 1 char (not the whole line), and the char you write is probably not what you expect.

    Fourth, having modified temp with temp[i] = ' '; you may as well stick with it. Copying it to finish doesn't add much.

    Code:
        	   if( temp[79] == '\n' ){
                   temp[80] == '0';
               }
    This can never be true - the last element of an array passed to fgets() will either be undefined or \0
    Furthermore, your attempt to modify temp[80] is both out of bounds of the array, and is a useless comparison (==), not an assignment.
    It's also a waste of effort, since fgets() always appends a \0 anyway.

    > for( i = 0; i < 80; i++){
    Where is it written that every line fills the buffer
    for ( i = 0 ; temp[i] != '\0'; i++ )
    examines only the valid characters.

    Finally, watch the precedence of && and ||
    I doubt your conditional does what you want it to.

    Simplify the code by writing an "isPunct()" function to do most of what that bulky if statement does.
    Code:
    if ( isPunct( temp[i]) ) {
      temp[i] = ' ';
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM