C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2010, 04:45 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 74
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.
thescratchy is offline   Reply With Quote
Old 03-18-2010, 04:47 AM   #2
Registered User
 
Join Date: Feb 2010
Posts: 74
im sorry i forgot to mention i
i am using fgets to read data from the file, so i can work with strings
thescratchy is offline   Reply With Quote
Old 03-18-2010, 04:48 AM   #3
Registered User
 
Join Date: Jul 2009
Location: Croatia
Posts: 261
Quote:
How can i say print everything from the string until that pointer adres.
Code:
while(p < p + number_of_chars)
    putc(*p, file);
Tool is offline   Reply With Quote
Old 03-18-2010, 04:59 AM   #4
Registered User
 
Join Date: Feb 2010
Posts: 74
OH my god: So simple and yet i couldnt see it

Thank you very much. I got it to work
thescratchy is offline   Reply With Quote
Old 03-18-2010, 05:44 AM   #5
Registered User
 
Join Date: Nov 2008
Location: INDIA
Posts: 60
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;
}
karthigayan is offline   Reply With Quote
Old 03-18-2010, 06:22 AM   #6
Registered User
 
Join Date: Feb 2010
Posts: 74
Thank you
thescratchy is offline   Reply With Quote
Old 03-18-2010, 06:51 AM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,292
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 03-18-2010, 06:54 AM   #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?
rabidbadger is offline   Reply With Quote
Old 03-18-2010, 12:38 PM   #9
Registered User
 
Join Date: Jul 2009
Location: Croatia
Posts: 261
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.
Tool is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help Fixing My C Program. Deals with File I/O Matus C Programming 7 04-29-2008 07:51 PM
gcc link external library spank C Programming 6 08-08-2007 03:44 PM
Inventory records jsbeckton C Programming 23 06-28-2007 04:14 AM
I'm not THAT good am I? indigo0086 General Discussions 2 10-19-2006 10:08 AM
Encryption program zeiffelz C Programming 1 06-15-2005 03:39 AM


All times are GMT -6. The time now is 12:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22