473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to replace a word in a file.

This is the code i have written to replace a line which contains GATEWAY.


Expand|Select|Wrap|Line Numbers
  1. #include<stdlib.h>
  2. #include <string.h>
  3. #define IF_CONF "ifffcccccc.txt"
  4. main()
  5. {
  6.         char line[130],line1[130];
  7.  
  8.         FILE *fp=NULL,*ofp=NULL;
  9.         fp=fopen(IF_CONF,"rw+");
  10.         ofp=fopen(IF_CONF,"rw+");
  11.         if(fp==NULL)
  12.         {
  13.                 printf("\n\nerror\n");
  14.                 return -1;
  15.         }
  16.         while (fgets(line,sizeof(line),fp))
  17.         {
  18.  
  19.                 printf("Line Read: %s\n",line);
  20.                 if(strstr(line, "GATEWAY") != NULL)
  21.                 {
  22.                         fclose(fp);
  23.                         printf("Replacing\n");
  24.                         strcpy(line,"GATEWAY=");
  25.                         strcat(line,"111.111.111.111\n");
  26.                         printf("%s",line);
  27.                 fseek(ofp,0,SEEK_CUR);
  28.                 fputs(line,ofp);
  29.                 printf("\nReplace Over\n");
  30.                 break;
  31.  
  32.                 }
  33.  
  34.  
  35.                 }
  36.  
  37.                 fgets(line1,sizeof(line1),ofp);
  38.         }
  39.  
  40. printf("The line = %s\n\n\n",line);
  41.         fclose(ofp);
  42. return 0;
  43. }
  44.  




The problem it is deleting the next line charecters.


The file "ifffcccccc.txt" contains
AAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBB
DDDDDDDDDDDDDD
GATEWAY=191.1.51.21
HELLO
HKKKKKKK



Please any one help..
Sep 5 '07 #1
5 2347
sicarie
4,677 Expert Mod 4TB
You got this to compile? What compiler/version? I can't even get it to compile with cygwin gcc v 3.4.4
Sep 5 '07 #2
xoinki
110 100+
hi,
u cant modify contents of a file once it is open.. wat u can do it rewrite the whole content once again.. and fclose the file.

Regards
Xoinki
Sep 5 '07 #3
You got this to compile? What compiler/version? I can't even get it to compile with cygwin gcc v 3.4.4

ya i am able to complile it..
Sep 6 '07 #4
hi,
u cant modify contents of a file once it is open.. wat u can do it rewrite the whole content once again.. and fclose the file.

Regards
Xoinki

I am able to modify it , but the problem is , i am not able to write to a line more than the string length of the original contents of the line.if i write like that the next line contents is overwritten.
Sep 6 '07 #5
sicarie
4,677 Expert Mod 4TB
I am able to modify it , but the problem is , i am not able to write to a line more than the string length of the original contents of the line.if i write like that the next line contents is overwritten.
Right, well, apparently you're not going to share your compiler or version, so why not read the entire file in, close it (still holding the file contents in memory), and then open a new file output stream and write the data you need?
Sep 6 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: - ions | last post by:
Hi, i would like to know how to replace every char in a string with a certin given char using the String.replace(char oldChar,char newChar). I would like to replace all letters with an underscore...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
1
by: adam lital | last post by:
Hi, I have word document and i want to replace a specific text with a bookmark using code. Example: in the document i have the text and i want to replace this to a bookmark called ClientName....
0
by: Balakrsihna | last post by:
Hi All, Can anyone tell me how to replace an image in MS Word 2003 using c#.net. I am converting an html file to word doc and sending this as attachment using System.Web.Mail; the...
1
by: James Vitale | last post by:
Using vb asp.net 1.1 I'm doing a word automation on a doc file and trying to do a find and replace. My existing code works fine except that it doesn't find and replace in the header. My code...
5
by: Casey | last post by:
Hello, Can someone give me specific code to replace text on a page using server side javascript? I need to use server-side because I need the output to be recognized in the final HTML so that...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
1
by: Michael Yanowitz | last post by:
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the...
2
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
4
by: senthil2ganesh | last post by:
How to replace a word in a file contain with other word.For example If the file contain word like %,This % is should replace with %.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.