Format text(ifstream, ofstream)

  • Thread starter Thread starter ghost305
  • Start date Start date
  • Tags Tags
    Format
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to reading and formatting text from a source file using C++. Participants are exploring methods to remove excess whitespace and properly format the text output while addressing specific challenges encountered in the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a program that reads a text file and attempts to format it by removing excess whitespace, but is encountering issues with the output retaining unwanted spaces.
  • Another participant asks for clarification on what the program actually displays, emphasizing the need for specific symptoms of the problem.
  • Some participants suggest considering alternative approaches to reading the file, such as using the >> operator, which inherently skips whitespace and may help in formatting the text correctly.
  • There is a mention of the need to preserve newlines while reformatting the text to a maximum line length, indicating that the current approach may not be sufficient.
  • One participant expresses difficulty in implementing a solution that maintains newlines and requests assistance with error messages encountered during their attempts.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to solve the formatting issue, with multiple competing views on how to handle whitespace and newlines in the text processing.

Contextual Notes

There are unresolved technical challenges related to maintaining the structure of the text while removing excess whitespace, and participants have not agreed on a definitive method to achieve the desired output.

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?
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K