| | |
Remove extra whitespace
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 16
Reputation:
Solved Threads: 0
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
This is my input
hw4pr6input.txt
This is my current output
hw4pr6output.txt
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)
#include <iostream> #include <iomanip> #include <fstream> #include <cstdlib> using namespace std; void check_blank(ifstream& instream, ofstream& outstream); int number = 0; //keeps track of how many spaces have been deleted int main() { ifstream instream; ofstream outstream; bool t; instream.open("hw4pr6input.txt"); //Open file: text to be edited if(!instream.fail()) //Make sure file opened successfully { cout << "test from open"; //Call function that finds blanks and edits } else { cout << "File failed to open (1st)"; exit(1); } outstream.open("hw4pr6output.txt"); //Open file: edited text will go if(!outstream.fail()) //Check if open failed { t = true; check_blank(instream, outstream); //Call function to input edited text into file } else { cout << "File failed to open (2nd)"; exit(1); } cout << "Removed " << number << " blanks from file" << endl; system("PAUSE"); instream.close(); //Close both files outstream.close(); return 0; } //End main void check_blank(ifstream& instream, ofstream& outstream) { char a; int count = 0; //Variable tells if there are multiple blank spaces while(! instream.eof()) //While the file isn't at the end { instream.get(a); if(a == ' ') //If the input is a space { number++; count++; //Add 1 to number & count if(a == ' ' && count >= 2) //If there are 2 or more spaces then do this { outstream << ' '; count = 0; //reset count } //end inner if } //end outer if else { outstream << a; } }//end while }//end check function
This is my input
hw4pr6input.txt
This is my current output
hw4pr6output.txt
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)
string word; while( infile >> word) 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.
•
•
•
•
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)
string word; while( infile >> word) outfile << word << " ";
"Make simple things easy."
>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:
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.
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)
char last = 0; char ch; while ( in.get ( ch ) ) { if ( ch == ' ' && last == ' ' ) continue; out.put ( ch ); last = ch; }
New members chased away this month: 5
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.
![]() |
Similar Threads
- Not waiting for input during loop (C++)
- What to do if the string entered exceeds the specified size (C)
- This ought to be simple - extra spaces (PHP)
Other Threads in the C++ Forum
- Previous Thread: What its the use of using namespace std;
- Next Thread: unexpected behaviour
Views: 3205 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






