473,383 Members | 1,859 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,383 software developers and data experts.

Search and replace char in same text file

8
Hi there,

I've got this piece of code, but I'm having trouble getting it to write the changes into the file.

What I'm trying to do is:
  • Read text file
  • Find user specified character (eg. what to replace all the a's)
  • Replace the character (eg. replace all a's with b's)
  • Write to the same text file

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_LEN_SINGLE_LINE     1000
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.         if (argc < 4)
  9.         {
  10.                 fprintf(stderr, "Format should be: %s  char to replace  char replacing\n", argv[0]);
  11.                 return 1;
  12.         }
  13.  
  14.     char buffer[MAX_LEN_SINGLE_LINE+2];
  15.     char *buff_ptr, *find_ptr, temp;
  16.     FILE *fp1;
  17.     size_t find_len = strlen(argv[2]);
  18.  
  19.     fp1 = fopen(argv[1],"r");
  20.  
  21.     while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
  22.     {
  23.         buff_ptr = buffer;
  24.         while ((find_ptr = strstr(buff_ptr,argv[2])))
  25.         {
  26.             while(buff_ptr < find_ptr)
  27.                 fputc((int)*buff_ptr++,fp1);
  28.  
  29.             fputs(argv[3],fp1);
  30.             buff_ptr += find_len;
  31.         }
  32.         fputs(buff_ptr,fp1);
  33.     }
  34.  
  35.     fclose(fp1);
  36.  
  37.     return 0;
  38. }
  39.  
At the moment it compiles fine (with a few warnings) but when I check the file it does not have the changes made to it.

User would input something like: ./a.out myfile.txt a b
(that is - take myfile.txt - replace all a's and replace with b's)

Any help would be greatly appreciated!
Apr 16 '09 #1
2 7151
donbock
2,426 Expert 2GB
On line 19 you open the file for reading ("r"). Try opening it for update ("r+"). Refer to the fopen man page for details.
Apr 16 '09 #2
anii
8
Hi donbock, thanks for your reply. I've changed the ("r") to ("r+") and tweeked the rest of the code to rewind and little and write.
Apr 16 '09 #3

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

Similar topics

0
by: todd | last post by:
here is a search tool SP I wrote. How many times have you wanted to search all your Stored procs or views (in a database) for a keyword but couldn't!? Well now you can! THis can makes life a...
10
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
10
by: B. Williams | last post by:
I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called...
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,...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.