Remove extra whitespace

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 16
Reputation: Squeeker is an unknown quantity at this point 
Solved Threads: 0
Squeeker Squeeker is offline Offline
Newbie Poster

Remove extra whitespace

 
1
  #1
Feb 14th, 2008
Hello all,

I am working on a program that takes input from a file, removes extra white spaces and outputs the edited text to another file.

So far my program deletes 1 whitespace whether the whitespace should be deleted or not.

I have read about peek() and putback(). I am pretty sure I need to implement these two functions but quite frankly I am not sure how to go about doing this.

I don't want anyone to just give me the answer I would really appreciate it if someone gave me a hint as to where my logic went south or my structure or enlighten me about some predefined function you think I may not be aware of.

Any help is appreciated. Thank you.
-Arielle

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7. void check_blank(ifstream& instream, ofstream& outstream);
  8. int number = 0; //keeps track of how many spaces have been deleted
  9.  
  10. int main()
  11. {
  12. ifstream instream;
  13. ofstream outstream;
  14. bool t;
  15.  
  16. instream.open("hw4pr6input.txt"); //Open file: text to be edited
  17. if(!instream.fail()) //Make sure file opened successfully
  18. {
  19. cout << "test from open";
  20. //Call function that finds blanks and edits
  21. }
  22. else
  23. {
  24. cout << "File failed to open (1st)";
  25. exit(1);
  26. }
  27.  
  28. outstream.open("hw4pr6output.txt"); //Open file: edited text will go
  29. if(!outstream.fail()) //Check if open failed
  30. {
  31. t = true;
  32. check_blank(instream, outstream); //Call function to input edited text into file
  33. }
  34. else
  35. {
  36. cout << "File failed to open (2nd)";
  37. exit(1);
  38. }
  39.  
  40. cout << "Removed " << number
  41. << " blanks from file"
  42. << endl;
  43.  
  44. system("PAUSE");
  45. instream.close(); //Close both files
  46. outstream.close();
  47.  
  48. return 0;
  49. } //End main
  50.  
  51. void check_blank(ifstream& instream, ofstream& outstream)
  52. {
  53. char a;
  54.  
  55. int count = 0; //Variable tells if there are multiple blank spaces
  56.  
  57. while(! instream.eof()) //While the file isn't at the end
  58. {
  59. instream.get(a);
  60. if(a == ' ') //If the input is a space
  61. {
  62. number++;
  63. count++;
  64. //Add 1 to number & count
  65. if(a == ' ' && count >= 2) //If there are 2 or more spaces then do this
  66. {
  67. outstream << ' ';
  68. count = 0; //reset count
  69. } //end inner if
  70. } //end outer if
  71. else
  72. {
  73. outstream << a;
  74. }
  75.  
  76. }//end while
  77. }//end check function

This is my input
hw4pr6input.txt

This is my current output
hw4pr6output.txt
Attached Files
File Type: txt hw4pr6input.txt (742 Bytes, 12 views)
File Type: txt hw4pr6output.txt (667 Bytes, 10 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Remove extra whitespace

 
0
  #2
Feb 14th, 2008
why don't you do this the easy way by using the ifstream >> operator to remove all white space between words with output the word just read with one space
  1. string word;
  2. while( infile >> word)
  3. outfile << word << " ";
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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: Remove extra whitespace

 
0
  #3
Feb 14th, 2008
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Remove extra whitespace

 
0
  #4
Feb 14th, 2008
Originally Posted by Ancient Dragon View Post
why don't you do this the easy way by using the ifstream >> operator to remove all white space between words with output the word just read with one space
  1. string word;
  2. while( infile >> word)
  3. outfile << word << " ";
Like Larry Wall said:

"Make simple things easy."
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: Remove extra whitespace

 
2
  #5
Feb 14th, 2008
>Make simple things easy.

Here, here. But what about the newlines and/or tabs?
Last edited by iamthwee; Feb 14th, 2008 at 5:47 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,628
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 717
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Remove extra whitespace

 
0
  #6
Feb 14th, 2008
>But what about the newlines and/or tabs?
Indeed. And what about such monstrosities as "\t \n\n\t"? Does "extra whitespace" refer to adjacent whitespace characters of the same value, any value that isspace returns true for, or are we only working with the ' ' character? The problem doesn't seem to be well defined.

>I have read about peek() and putback().
Unnecessary. You can save the last character read from the stream and use it as a state to process the next character:
  1. char last = 0;
  2. char ch;
  3.  
  4. while ( in.get ( ch ) ) {
  5. if ( ch == ' ' && last == ' ' )
  6. continue;
  7.  
  8. out.put ( ch );
  9. last = ch;
  10. }
A good rule of thumb is that if you think you need to peek or putback on a stream, you probably need to improve your design.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Remove extra whitespace

 
0
  #7
Feb 14th, 2008
tabs are considered white space so they are treated just like spaces. But you (jamthwee) are correct about newlines, so my previous suggestion will not work. But there is hope. Use getline() to read the entire line then stringstream to split it up into individual words similar to what I posted before. I was about to post the solution, but then I would be doing the OPs homework for him wouldn't I ?
Last edited by Ancient Dragon; Feb 14th, 2008 at 9:53 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: Squeeker is an unknown quantity at this point 
Solved Threads: 0
Squeeker Squeeker is offline Offline
Newbie Poster

Re: Remove extra whitespace

 
0
  #8
Feb 14th, 2008
Thank you for all the pointers everyone. I really appreciate it.
-Arielle
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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