Format text(ifstream, ofstream)

  • Thread starter Thread starter ghost305
  • Start date Start date
  • Tags Tags
    Format
AI Thread Summary
The discussion centers on a programming issue involving reading and formatting a text file while removing excess whitespace. The user is attempting to modify their code to ensure that the output displays words with a single space between them, rather than preserving the original formatting with irregular spacing. Suggestions include using the `>>` operator for reading input, which automatically handles whitespace, and considering line length limitations for proper formatting. The conversation highlights the need for a different approach than line-by-line reading to achieve the desired output. The user is encouraged to share specific errors encountered when trying to implement these suggestions for further assistance.
ghost305
Messages
14
Reaction score
0
I'm having problem with a program I am 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.
 
Technology news on Phys.org
ghost305 said:
IOutput 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.

What does it actually look like when you run the program?
 
jtbell said:
What does it actually look like when you run the program?

No what I am saying is that the original text looks like this:
The
kid is playing on
the...



-------------------
The program is supposed to make it look like this;
The kid is playing...
 
And I'm asking you, what does your program actually display when you run it, instead of the desired output?

When you visit a doctor, you don't simply say, "I'm sick." You tell him what your symptoms are. :smile:

So, what are the symptoms of your sick program?
 
jtbell said:
And I'm asking you, what does your program actually display when you run it, instead of the desired output?

When you visit a doctor, you don't simply say, "I'm sick." You tell him what your symptoms are. :smile:

So, what are the symptoms of your sick program?
the program runs, it displays the text like it is on source text with all the excess whitespace. What I'm trying to do is remove the excess whitespace and edit the text so there is one " " between each words. in other words, formatting the text.
 
OK, then you might want to consider a different approach than reading line by line, unless of course you are required to read line by line.

Consider the standard behavior of the >> operator. If you have this:

string s;
input >> s;

what happens in detail is that:

1. If the current position in the file contains a whitespace character, it skips that character and the following consecutive whitespace characters until it finds a non-whitespace character.

2. Then it reads that non-whitespace character, and the following consecutive non-whitespace characters, into 's'.

3. Finally, it stops reading when it finds a whitespace character, and leaves that whitespace character in the input stream for the next input operation to deal with.

Does this give you any ideas?
 
jtbell said:
OK, then you might want to consider a different approach than reading line by line, unless of course you are required to read line by line.

Consider the standard behavior of the >> operator. If you have this:

string s;
input >> s;

what happens in detail is that:

1. If the current position in the file contains a whitespace character, it skips that character and the following consecutive whitespace characters until it finds a non-whitespace character.

2. Then it reads that non-whitespace character, and the following consecutive non-whitespace characters, into 's'.

3. Finally, it stops reading when it finds a whitespace character, and leaves that whitespace character in the input stream for the next input operation to deal with.

Does this give you any ideas?
Code:
  if( input >> buff ) {
    cout << buff;
    while ( input >> buff )
      cout << ' ' << buff;
  }

I did this but it ignored newline character as well. Parts of the words at the end of a line show up on the next line kinda like this:
the mor
ning
 
Your original example didn't preserve the newlines. If you want to reformat the lines to a new maximum line length, then you have to keep a count of how many chars your current line contains, and output a newline instead of a space when the count reaches the maximum.
 
jtbell said:
Your original example didn't preserve the newlines. If you want to reformat the lines to a new maximum line length, then you have to keep a count of how many chars your current line contains, and output a newline instead of a space when the count reaches the maximum.
How do i do that?
everytime i try to do it i get an error
 
  • #10
Can you show us what you tried, and what the error message was?
 
Back
Top