Thread: Change word in a file

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    Change word in a file

    Hello everyone

    I have a file :

    Code:
    <html> 
    <head> 
    <t i t l e>UHasselt</ t i t l e> 
    </head> 
    <body>
    <font color = ”red”>Dit is de website van de UHasselt</font> 
    <br/> 
    <br/> 
    <font color = ”#123456”>Info over de universiteit</font> 
    <br/>
    <br/> 
    <a	href=”http://www. uhasselt .be”>link</a>
    </body> 
    </html>
    And i want to change the color #123456 into the word green.

    Now i know how to go threw the file and write the file.
    But how can i change the word? I thought something like this :

    I take the length of the word #123456, and with the function strstr() i look at the pointer of the first place. And now i am stuck.

    How can i say print everything from the string until that pointer adres.
    And after concatenating the word green to this string print everything else after the wordt #123456.

    I really appreciate some help. I have examination tomorrow.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    im sorry i forgot to mention i
    i am using fgets to read data from the file, so i can work with strings

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    How can i say print everything from the string until that pointer adres.
    Code:
    while(p < p + number_of_chars)
        putc(*p, file);

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    OH my god: So simple and yet i couldnt see it

    Thank you very much. I got it to work

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up

    Use this code,

    Code:
    #include<stdio.h>
    #include <string.h>
    #include<stdlib.h>
    char *replace(char *str, char *from, char *to);
    
    main()
    {
            char buf[1024];
            FILE *fp;
            char *p;
    
            if((fp=fopen("file","r+"))==NULL)
            {
                    perror("fopen:");
                    exit(1);
            }
            else
            {
                    while(fgets(buf,sizeof(buf),fp)!=NULL)
                    {
                            printf("%s",buf);
                    if((p=strstr(buf,"#123456"))!=NULL)
                    {
                            printf("%s\n",replace(buf, "#123456", "green"));
                    }
                    }
            }
    
    }
    
    
    char *replace(char *str, char *from, char *to)
    {
      static char buffer[1024];
      char *p;
    
      if(!(p = strstr(str, from)))
        return str;
    
      strncpy(buffer, str, p-str);
      buffer[p-str] = '\0';
    
      sprintf(buffer+(p-str), "%s%s", to, p+strlen(from));
    
      return buffer;
    }

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That only replaces the word in-memory, not in-file. You cannot simply seek to a spot, find a word, and overwrite it, without rewriting the entire remainder of the file from that point on, unless the words are the exact same length (or unless the source word is longer than the target word AND you're adding in some form of padding.
    Code:
    #123456
    green
    Notice how they are not at all the same length. That means if all you do (and you're not, in the code above, you aren't doing anything to the word on disk) if seek to a spot and write out the word "green", that you'll end up with "green56" on disk. And if it were reversed, you'd be overwriting "green" with a longer word, which would mung up two extra bytes in your file.


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

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Quote Originally Posted by karthigayan View Post
    Use this code
    Erm, that code won't actually do what the OP asked for. It will output up to the line with the search string in, the line with the search string in, the same line with the string replaced, and then the rest of the file. As it's a simple fix, I don't know if you made the error for the learning benefit of the OP or not?

  9. #9
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Yeah Quzah is right.

    Open the wanted file for reading, then open another file for writing - you read data from the first file, write it to the second one according to readen data in the first.

    After your done, you could open the first file for writing, copy all data from the 2nd file and then delete the 2nd file which you no longer need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. 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
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM