Thread: search / repalce

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

    search / repalce

    i'm trying to make a function, which is searching a string in file. If the string exist it must be replaced with another one.
    what is my idea...
    I'm inserting string for searching
    with fgets I'm reading all line-to-line content of the file
    with strstr I'm checking if the searching string is exists in the file
    if the string exist I'm inserting the new string
    with freopen I'm closing the file and make new empty file with the same name
    with fputs I'm writing the readed to this moment + the new string
    And then I don't know how to continue
    I'm posting my source code ( when I try to execute it it stops at while and says that fgets == NULL )

    Code:
    int update_field(char *phone)
    
    {
    
    	FILE *fp = fopen("names.txt", "a");
    	char buff[60];
    	char new_phone[20];
    	
    	if ( fp == NULL ) {
    		printf("update_field/fp");
    		exit(0);
    	}
    
    	else {
    		while ( fgets(buff, sizeof buff, fp) != NULL) {
    			if ( strstr(buff, phone) != NULL) {
    				printf("Type the new phonenumber");
    				gets(new_phone);
    				fp = freopen("names.txt", "w", fp);
    				fputs(buff, fp);
    				fputs(new_phone, fp);
    				fclose(fp);
    			}
    		}
    	}
    }
    can u help me?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can u help me?
    Try something more like this:
    Code:
    /* Return non-zero for success, zero for failure */
    int update_field(char *phone, char *new_phone)
    {
        FILE *file;
        FILE *temp;
        FILE *save;
        char buff[BUFSIZ];
    
        if ((file = fopen("names.txt", "r")) == NULL) {
            perror("update_field -- fopen");
            return 0;
        }
    
        if ((temp = tmpfile()) == NULL) {
            perror("update_field -- tmpfile");
            fclose(file);
            return 0;
        }
    
        while (fgets(buff, sizeof buff, file) != NULL) {
            if (strstr(buff, phone) != NULL)
                fputs(new_phone, temp);
            else
                fputs(buff, temp);
        }
    
        if ((save = freopen("names.txt", "w", file)) == NULL) {
            perror("update_field -- freopen");
            fclose(file);
            fclose(temp);
            return 0;
        }
    
        file = save;
        rewind(temp);
    
        while (fgets(buff, sizeof buff, temp) != NULL)
            fputs(buff, file);
    
        fclose(file);
        fclose(temp);
    
        return 1;
    }
    My best code is written with the delete key.

  3. #3
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    Code:
    void searchandreplace(char* s, char* r)
    {
    	char *pin,*pout,........tart;
    
    	pin = &inline[0];
    	pout = &outline[0];
    	strcpy(pout,pin);
    
    	sstart = strstr(outline,s);
    	if (sstart == NULL) return;
    	foundmatch = True;
    	pin = r;
    	pout = sstart;
    
    	x = strlen(r);
    	while (x > 0)
    	{
    
    	    *pout = *pin;
    	     pin++; pout++;
    	     x--;
    	}
    
    	pin = strstr(inline,s); 
    	pin += strlen(s);
    	strcpy(pout,pin);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM