Format text(ifstream, ofstream)

This is a discussion on Format text(ifstream, ofstream) within the C++ Programming forums, part of the General Programming Boards category; I'm having problem with a program im trying to write that reads a .text sourcefile and then format the .text ...

  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
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,445
    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 << ' ';
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 3 123 LastLast
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, 12:10 PM
  5. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21