Removing a particular character from a text file and replacing it

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 763 Views - Last Post: 16 July 2011 - 11:07 AM Rate Topic: **--- 3 Votes

#1 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 12:49 AM

i want to remove a particular character from the text file "Temp.txt" and replace that with a 'space' character. (' ')
so here is what code i have come up with and it just isn't working :helpsmilie:
and also i want to replace ONLY one of the specific character in the line , if there are more characters it should be ignored.
void remove(char chs,int lineno)
{
   fstream f("Temp.txt",ios::in|ios::out);
   char line[80];int lno=1; // starting line number 
   while(f.getline(line,80))
   {
      int count=0;
      for(int k=0;k<80 && lno==lineno;k++)
      {
   	if(line[k]==chs && count==0)
      	{
      		char x=' ';
      		f.seekg(-1,ios::cur);
         	f<<x;count++;
      	}
      }
      lno++;
   }
   f.close();
}



:helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Removing a particular character from a text file and replacing it

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 01:59 AM

hmmmm, lots to talk about here.

Let's start by having you explain what this line does and why you chose to do those things the way you did.
 for(int k=0;k<80 && lno==lineno;k++)



When you say out loud what it is doing does another way of achieving your aim come to mind?
Was This Post Helpful? 0
  • +
  • -

#3 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 02:34 AM

the line

 for (int k=0;k<80 && lno==lineno;k++) 


i used it to assuming that the DOS screen that we get on running the code has 80 characters per line, the variable 'lno' is used to calculate the number of lines. and 'lineno' contains the line number from which the character has to be removed.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 02:40 AM

And why are you testing the line number in the same test as looping through the characters in a line?
Why? Why? Why?
Was This Post Helpful? 0
  • +
  • -

#5 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 02:46 AM

because i need to replace the chosen character in the particular line , so i am looping it get to the particular line and then checking character by character to replace !
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 02:51 AM

Sorry I wasn't clear enough.
I have made the important part of the question easier for you to see.

View Postjanotte, on 16 July 2011 - 08:40 PM, said:

And why are you testing the line number in the same test as looping through the characters in a line?
Why? Why? Why?

Was This Post Helpful? 0
  • +
  • -

#7 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 02:54 AM

ok got your point , so i modified the code this way , what corrections do i need to make

void remove(char chs,int lineno)
{
   fstream f("Temp.txt",ios::in|ios::out);
   char line[80];int tno=1,replaced=0;
   while(f.getline(line,80))
   {
      if(tno==lineno)
      {
      	for(int k=0;k<80;k++)
            if((line[k]==chs)&&(replaced==0))
            {
            	f.seekg(-1,ios::cur);
               char x=' ';
               f<<x;
               replaced++;
            }
      }
      tno++;
   }
   f.close();
}


Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 03:02 AM

And what is your bug report?

Does your code compile?
Does it run?
If it runs how is the output/outcome different to what you want?

This post has been edited by janotte: 16 July 2011 - 03:04 AM

Was This Post Helpful? 0
  • +
  • -

#9 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 03:08 AM

the code compiles , it runs but doesn't give the required output , it doesn't replace any characters

:rolleyes2:
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 03:58 AM

void remove(char chs,int lineno) {
	fstream f("Temp.txt",ios::in|ios::out);
	// why not string??
	// in any case, magic numbers bad
	const int MAX_LINE = 80;
	char line[MAX_LINE];
	int tno=1,replaced=0; // tno, why not zero indexed, everything in C++ is zero indexed
	
	// you read a number of character into your buffer
	// you also assume a line is no more than MAX_LINE long
	// this is where a string could help...
	while(f.getline(line,MAX_LINE)) {
		// you've read up to your line
		if(tno==lineno) {
			// you are assuming you have exatly MAX_LINE size
			// which is probably wrong
			for(int k=0;k<MAX_LINE;k++) {
				// you find a char in the c-string
				// btw, why do you care if replaced==0?
				if((line[k]==chs)&&(replaced==0)) {
					// um, you're moving back on position in the file
					// what does this have to do with line?
					// shouldn't you move have some relation to line?
					f.seekg(-1,ios::cur);
					// ok...
					char x=' ';
					f<<x;
					replaced++;
            }
			}
		}
		tno++;
	}
	f.close();
}



You could have stopped reading much earlier. Also, you might need to read line to get to lineno, but after that you might consider reading char.
Was This Post Helpful? 0
  • +
  • -

#11 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 04:38 AM

baavgai the code you have given is being compiled but still doesn't give me the desired output.
Was This Post Helpful? -1
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 04:57 AM

The code I gave is your code, with comments to help you figure out what's wrong. If you just take any code that's posted and compile it in the hope someone just did it for you, I'm afraid you're doomed.
Was This Post Helpful? 1
  • +
  • -

#13 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 05:17 AM

Sorry man , i tried everything , just can't figure it out :nottalkingtoyou:

some more hints would do ....
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 05:54 AM

Honestly, just read characters. It's probably easier.

Read every character, counting the '\n' as you go. When you're on the line, that is have read X number of '\n', keep reading characters. If the character is the one you're looking for, then do the seek -1 to overwrite it. When you're done with the line, just leave the loop.

That's the logic of one solution, you need only implement it in code. Good luck.
Was This Post Helpful? 0
  • +
  • -

#15 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Removing a particular character from a text file and replacing it

Posted 16 July 2011 - 06:04 AM

Thanks :gunsmilie:

GOT IT NOW
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2