Thread: Scan some text and replace words

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

    Scan some text and replace words

    I am having some problems on this one.

    I want to read a text and everytime that it reads the word "test four", I want to replace it with the word "view five".


    I came up with this code, using the FAQ on this site. Anyone know what I do terribly wrong?

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <math.h>
    
    #define MAX_LINE (128+1)  /* Maximum length of a source line plus one */
    
    
    int main()
    {
        char InputBuffer[MAX_LINE];
    
        FILE *Infile;
        char *string[MAX_LINE];
    
    
        if ((Infile = fopen("input.txt", "r")) == NULL) {
            fprintf(stderr, "Error opening input-file :%s\n", "input.txt");
            exit(1);
        }
    
        while (!feof(Infile)) {
    	(fgets(InputBuffer, MAX_LINE, Infile));
    		if ((string = strstr(InputBuffer, "test four")) !=NULL) 
    		*string = "view five";
            	printf( "%s\n",InputBuffer);
        }
    return(0);
    
    fclose (Infile);
    }

    I used strstr.. because K&R says that it is the same as strchr, except that it is not for characters, but for strings.
    Last edited by Killroy; 11-22-2004 at 06:36 AM. Reason: Too big + error in code

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're trying to replace the text incorrectly. First off, do you mean to have an array of character pointers? Or is that just supposed to be a buffer?

    Second, you will need to use something like strcpy to copy one string over the other. Or, simply loop through the characters and replace them that way. Something like:
    Code:
    s = strstr( inputbuffer, "test four" );
    if( s )
    {
        char buf[] = "view five", *p;
        for( p = buf; *p; *s++ = *p++ );
    }
    Now this will only safely work because the string you want to replace is exactly the same length as the one you want to replace it with. You'll have to do a bit more to replace strings of other sizes. Note, this also does nothing for the file itself, it's simply an in-memory replacement.

    I'll leave the different size replacement as an exercize to the reader.

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

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    17
    Quzah wrote
    Code:
    You're trying to replace the text incorrectly. First off, do you mean to have an array of character pointers? Or is that just supposed to be a buffer?
    That is indeed my fault.. I just want it to read some text and when it finds a reserved word of for example 9 char, it needs to replace it with another string of 9 chars..


    I now got it to run, with indeed.. strcpy, but..

    Now it reads the text, and when it finds my reserved string, it replaces it, but ignores the rest of the line completely..

    Example

    Code:
    "It was confirmed that test four did not suit the \n
    requirements
    initially planned. To gain a better understandment.. etc."
    has output
    Code:
    "view five \n
    initially planned. To gain a better understandment.. etc."

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    17
    btw... fixed the problem, and I understand your code (which is more important to me)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  2. Replies: 1
    Last Post: 04-24-2005, 03:45 AM
  3. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  4. Find and Replace Text Function
    By mart_man00 in forum C Programming
    Replies: 45
    Last Post: 03-13-2003, 10:21 PM
  5. How to replace strings in a file?
    By Doh in forum C Programming
    Replies: 6
    Last Post: 11-26-2002, 12:16 PM