File handling: How to search and replace strings in file using user input

Reply

Join Date: Sep 2008
Posts: 2
Reputation: hikaru1239 is an unknown quantity at this point 
Solved Threads: 0
hikaru1239 hikaru1239 is offline Offline
Newbie Poster

File handling: How to search and replace strings in file using user input

 
0
  #1
Sep 8th, 2008
I am working on a code which opens a file (infile), copies it to an output file (outfile) and then asks the user for a string and searches for that string in the outfile and replaces every occurence of that string with & symbol.

Here's an example:

Contents of infile:

Mary had a little lamb
Whose fleece as white as snow

//the program will copy the file to an output file and then ask the user for a string that it will search in that file.

//For example, the user wants to look for all the occurrences of the 'as'.
//the program will search for all occurrences of 'as' and replace it with the '&' symbol:

Mary had a little lamb
Whose fleece & white & snow

So far, this is what I was able to do:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define BUFF_SIZE 100
  5.  
  6. int main(int argc, char* argv[]) {
  7.  
  8. char mystring[500];
  9. char out[1000];
  10.  
  11. if (argc != 3) {
  12. fprintf(stderr, "Not enough parameters. Usage <progname> <infile> <outfile>\n");
  13. return -1;
  14. }
  15.  
  16. FILE* inFile = fopen(argv[1], "rt");
  17. FILE* outFile = fopen(argv[2], "wt");
  18. if(!inFile) {
  19. fprintf(stderr, "File not found! Exiting.\n");
  20. return -1;
  21. }
  22. if(!outFile) {
  23. fprintf(stderr, "File can't be opened for writing.\n");
  24. return -1;
  25. }
  26.  
  27. printf("Enter string to be searched:"); //asks the user to enter a string to be searched
  28. fgets(mystring, 500, stdin);
  29.  
  30. if(strlen(mystring) < 2){ //checks if the user entered a string
  31.  
  32. printf("You forgot to enter a string.");
  33. return -1;
  34. }
  35.  
  36. while(!feof(inFile)){
  37.  
  38. fgets(out, 1000, inFile); //copies the text in the input File
  39. fprintf(outFile, "%s", out); //prints the text from infile.txt to outfile.txt
  40.  
  41.  
  42. }
  43.  
  44.  
  45. return 0;
  46. }

I was only able to recopy the contents of the input file. I'm actually stuck with the search and replace file. Can you guys give me some tips. I was thinking of using strstr function but I'm still a bit lost. Also, can you give some example for this one? Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: File handling: How to search and replace strings in file using user input

 
0
  #2
Sep 8th, 2008
Have a look at the code snippets section for c.

Beware of using feof to read in files as well.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC