Thread: Format text(ifstream, ofstream)

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    72

    Format text(ifstream, ofstream)

    I'm having problem with a program im trying to write that reads a .text sourcefile and then format the .text file while removing excess whitespace.
    Code:
    #include <fstream>
    using std::ofstream;
    using std::ifstream;
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::cerr;
    #include <sstream>
    #include <iomanip>
    #include <cstring>
    using std::string;
    using namespace std;
    #include <cstdlib>
    using std::exit;
    #include <stdio.h>
    
    
    int main (void)
    {
    
    	const int MAX = 100;
    	char buff[MAX];
    	string name;
    	int i;
    	int countWords(const char string[]);
    	void readLine(char buffer[]);
    	
    	cout << "Enter name of input text file(location on disk):  "<<endl;  
    	getline (cin, name);				//Prompts user to input source text	
    
    	ifstream input;					//create stream object homework
    	input.open ( name.c_str() );		//reads source text file
    
    	if (!input) {
        cerr << "Unable to open file hw6.txt";
    	exit(1); }  // call system to stop
    	else{
    		while (!input.eof()) //until end of file
    		{
    			input.getline(buff, MAX); //read line of text
    			
    			cout << buff << endl;
    			} //display text(pre-formatted)
    	}
    
    	cout << "Enter name of the output text file:  " <<endl;
    	getline (cin, name); //Promt user to input output text
    
    	ofstream output;	//create stream object homework2
    	output.open (name.c_str() );
    
    	if(!output){
    	cerr << "Unable to create text file\n";
    	exit(1);} //call system to stop
    }
    I'm having issues formatting the input stream text file(removing excess whitespace.


    Demo text:

    The
    kid is
    playing in the field
    because he doesn't want to go inside.

    Output text(supposed to look like).
    It should look like this:
    The kid is playing in the field because he doesn't want to go inside.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of using std::getline(), use the overloaded operator>> for std::string, then print word by word.

    By the way, do not use using declarations (e.g., using std::ofstream) and using directives (e.g., using namespace std) before including a header as it could affect the contents of the header. You might also want to consider moving your function declarations before the definition of the main function, although in this case it probably does not matter.

    Also, generally you should not use eof() to control a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Also, generally you should not use eof() to control a loop.
    Code:
    while(getline(input, buff)){
         cout << buff << endl;
    }
    That's what I would use.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by Lorgon Jortle View Post
    Code:
    while(getline(input, buff)){
         cout << buff << endl;
    }
    That's what I would use.
    that what i use, but i don't know how to get the excess whitespace from the source text(input stream). Every word in there needs to be separated by one space.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackfraust
    that what i use, but i don't know how to get the excess whitespace from the source text(input stream). Every word in there needs to be separated by one space.
    As I suggested earlier, use the overloaded operator>> for std::string, then print word by word, e.g.,
    Code:
    while (input >> buff)
    {
        cout << buff << ' ';
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Or, to avoid having an extra blank space at the end:
    Code:
      if( input >> buff ) {
        cout << buff;
        while ( input >> buff )
          cout << ' ' << buff;
      }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    As I suggested earlier, use the overloaded operator>> for std::string, then print word by word, e.g.,
    Code:
    while (input >> buff)
    {
        cout << buff << ' ';
    }
    this approach will remove new lines as well replacing them with spaces. Not too good?

    Maybe using getline with stringstream parsing of the line is better?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vart View Post
    this approach will remove new lines as well replacing them with spaces. Not too good?
    According to the OP's example input/output, that's exactly what needs to happen.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by brewbuck View Post
    According to the OP's example input/output, that's exactly what needs to happen.
    I understand but some words at the end of the line are printed on the next line also. Like this:
    mor
    ning.

    ----------------------

    how about this?
    bool alphabetic ( const char c)
    {
    Last edited by jackfraust; 04-02-2009 at 10:46 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackfraust
    I understand but some words at the end of the line are printed on the next line also.
    That should not happen with what I suggested. Post your current code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    That should not happen with what I suggested. Post your current code.
    Code:
    #include <fstream>
    using std::ofstream;
    using std::ifstream;
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::cerr;
    #include <sstream>
    #include <iomanip>
    #include <cstring>
    using std::string;
    using namespace std;
    #include <cstdlib>
    using std::exit;
    #include <stdio.h>
    #include <iterator>
    
    int main (void)
    {
    
    	const int MAX = 100;
    	char buff[MAX];
    	string name;
    	int i;
    	int countWords(const char string[]);
    	void readLine(char buffer[]);
    	string FileData;
    	
    	cout << "Enter name of input text file(location on disk):  "<<endl;  
    	getline (cin, name);				//Prompts user to input source text	
    
    	ifstream input;					//create stream object homework
    	input.open ( name.c_str() );		//reads source text file
    
    	if (!input) {
        cerr << "Unable to open file hw6.txt";
    	exit(1); }  // call system to stop
    	else{
    // create reader objects
      if( input >> buff ) {
        cout << buff;
        while ( input >> buff )
          cout << buff << ' ';
      }
    }
    
    	cout << "\n\nEnter name of the output text file:  " <<endl;
    	getline (cin, name); //Promt user to input output text
    
    	ofstream output;	//create stream object homework2
    	output.open (name.c_str() );
    
    	if(!output){
    	cerr << "Unable to create text file\n";
    	exit(1);} //call system to stop
    	else{
    		
    		output.write(output >> buff);
    		output.close();
    	}
    	
    	
    	
    
    return 0;
    }
    it prints the words at the end of the line and prints the rest of the word on the next line. Also i need to input a range for the width of the format(from 40 to 100).

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackfraust
    it prints the words at the end of the line and prints the rest of the word on the next line.
    Oh, I think I know what you mean now. You are saying that the wrap around in the command prompt is such that a word is "broken" in the middle. That is perfectly fine, since eventually you will be printing to file, not the standard output stream, right?

    Quote Originally Posted by jackfraust
    Also i need to input a range for the width of the format(from 40 to 100).
    What do you mean?

    Next, among other things, you still need to:
    • Indent your code properly.
    • Move your using declarations and using directive (which you don't need) to after the includes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Oh, I think I know what you mean now. You are saying that the wrap around in the command prompt is such that a word is "broken" in the middle. That is perfectly fine, since eventually you will be printing to file, not the standard output stream, right?


    What do you mean?

    Next, among other things, you still need to:
    • Indent your code properly.
    • Move your using declarations and using directive (which you don't need) to after the includes.
    I need to prompt for max output width for the formatted text. lets say if the range(width for each line) is 70, i need to use a line of width 70 and the text can't be longer than that.

    So i intend to use:

    Code:
    cout << setfill ('o') << setw (70) << endl;
      cout << ' ' << buff << endl;
    but how do i setup buff to be 70 characters long without using (input.getline(buff, 70)?

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Could you count 70 chars, display then on a line, then count 70 more, all until the end of the file?

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by Lorgon Jortle View Post
    Could you count 70 chars, display then on a line, then count 70 more, all until the end of the file?
    Yes i can do that but when i do that, it counts all the excess whitespace too and we back to where i started again about removing them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  3. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  4. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 01:10 PM
  5. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 10:06 PM