![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 74
| Change word in a file 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> 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 | |
| | #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 | |
| | #3 | |
| Registered User Join Date: Jul 2009 Location: Croatia
Posts: 261
| Quote:
Code: while(p < p + number_of_chars)
putc(*p, file);
| |
| Tool is offline | |
| | #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 | |
| | #5 |
| Registered User Join Date: Nov 2008 Location: INDIA
Posts: 60
| 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 | |
| | #6 |
| Registered User Join Date: Feb 2010
Posts: 74
| Thank you |
| thescratchy is offline | |
| | #7 |
| +++ OK NO CARRIER 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 Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #8 |
| Registered User Join Date: Feb 2010
Posts: 11
| 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |