943,837 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5307
  • C++ RSS
Feb 14th, 2008
1

Remove extra whitespace

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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, 64 views)
File Type: txt hw4pr6output.txt (667 Bytes, 48 views)
Similar Threads
Reputation Points: 33
Solved Threads: 0
Newbie Poster
Squeeker is offline Offline
16 posts
since Feb 2008
Feb 14th, 2008
0

Re: Remove extra whitespace

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
C++ Syntax (Toggle Plain Text)
  1. string word;
  2. while( infile >> word)
  3. outfile << word << " ";
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Feb 14th, 2008
0

Re: Remove extra whitespace

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 14th, 2008
0

Re: Remove extra whitespace

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
C++ Syntax (Toggle Plain Text)
  1. string word;
  2. while( infile >> word)
  3. outfile << word << " ";
Like Larry Wall said:

"Make simple things easy."
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Feb 14th, 2008
1

Re: Remove extra whitespace

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 14th, 2008
0

Re: Remove extra whitespace

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 14th, 2008
0

Re: Remove extra whitespace

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Feb 14th, 2008
0

Re: Remove extra whitespace

Thank you for all the pointers everyone. I really appreciate it.
-Arielle
Reputation Points: 33
Solved Threads: 0
Newbie Poster
Squeeker is offline Offline
16 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: The very beginning
Next Thread in C++ Forum Timeline: unexpected behaviour





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC