8 Replies - 1222 Views - Last Post: 11 December 2009 - 09:28 AM Rate Topic: -----

#1 LilMike03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 09

Text Search and Delete

Post icon  Posted 12 October 2009 - 02:54 PM

I was given a C for dummies book and asked to produce the following:

- Search a text file for mulitple strings & if found delete the line that contains the string (from begining to end) and the following two lines.


- Replaced a section of text no matter what the values are and replace them with a preset value.

I'm not looking for anyone to write this for me..but after 6 hrs of looking here and google I'm not even finding a good starting point.

Help Please with a starting point.

Is This A Good Question/Topic? 0
  • +

Replies To: Text Search and Delete

#2 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Re: Text Search and Delete

Posted 12 October 2009 - 03:16 PM

Wait I need a little clarification here, it is asking you to search for a string and if it finds a certain string delete it?
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Text Search and Delete

Posted 12 October 2009 - 04:45 PM

Read in the file line by line
ifstream in("file.txt");
string line = "";
string yourWord = "Whatever";
while(getline(in,line))
{
	if(line.find(yourWord,0)!=string::npos)
	{
	 // do this
	}
}




EDIT** I just realized your doint this in C, the above is a method for C++


}
Was This Post Helpful? 0
  • +
  • -

#4 LilMike03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 09

Re: Text Search and Delete

Posted 13 October 2009 - 05:34 AM

What I have to modify is a configuration file where I need to find a specific string "Type=DS". I then need to remove the current line where the string is found and the following 2 lines.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Text Search and Delete

Posted 13 October 2009 - 05:46 AM

Seems pretty straightforward to me.
Read line
if line does not contain what you're looking for
	write line to new file
else
	read lines x 2 and ignore them


Was This Post Helpful? 0
  • +
  • -

#6 LilMike03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 09

Re: Text Search and Delete

Posted 13 October 2009 - 07:43 AM

I have got this far. No just need some guidance on

"else
read lines x 2 and ignore them
"

#include <stdio.h>
#include <stdlib.h>


int main()
{

	char line[256];
	FILE *config_old;
	FILE *config_new;
	char ch = 'a';
	char str[17] = "CardType=JC, Type"; /*Search String*/
	

	system("cls");
     config_new=fopen("config.cfg","r+");
     if(!config_new)exit(1);     
     else
	{
        while(ch!=EOF) 
		{
		line = fgetc(config_new);
		if(line == str)
		     { 
                                       ***Need Help Here to ommint line if string is found and the next 2 lines***
		     }
		}
	}
	return(0);
}

This post has been edited by JackOfAllTrades: 13 October 2009 - 08:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Text Search and Delete

Posted 13 October 2009 - 08:04 AM

fgets gets a string. All you're doing with fgetc is getting a character.

Hint: the function strstr() finds a string within another string
Was This Post Helpful? 0
  • +
  • -

#8 LilMike03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 09

Re: Text Search and Delete

Posted 14 October 2009 - 06:43 AM

still working this. I have so far been able to do the following:

- Create the backups needed
- Copy the current setup
- **working on doing the ommits for the new setup.**
- Write changes back into the program.
Was This Post Helpful? 0
  • +
  • -

#9 corndog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 04-December 09

Re: Text Search and Delete

Posted 11 December 2009 - 09:28 AM

View PostLilMike03, on 14 Oct, 2009 - 05:43 AM, said:

still working this. I have so far been able to do the following:

- Create the backups needed
- Copy the current setup
- **working on doing the ommits for the new setup.**
- Write changes back into the program.


I was curious if you had any experience with getting certain text from inside of a text box and replacing that text with other text. Say you had

THE CAR IS BROWN // THE HOUSE IS BROWN // THEY ARE BOTH BROWN

If you took this phrase and put it in a textbox, and then wanted to remove THE HOUSE IS BROWN and replace it with THE VAN IS BROWN.

Any help would be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1