Format text(ifstream, ofstream)

  • Thread starter ghost305
  • Start date
  • Tags
    Format
In summary: 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 new value.
  • #1
ghost305
14
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
  • #2
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?
 
  • #3
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...
 
  • #4
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?
 
  • #5
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.
 
  • #6
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?
 
  • #7
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
 
  • #8
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.
 
  • #9
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?
 

What is the purpose of using ifstream and ofstream to format text?

The purpose of using ifstream and ofstream is to read and write formatted text from a file in C++. This allows for easier manipulation and organization of data in a file.

What is the difference between ifstream and ofstream?

ifstream is used for reading input from a file, while ofstream is used for writing output to a file. They have different functions and syntax for their respective purposes.

How do you open a file using ifstream and ofstream?

To open a file using ifstream, you must first declare an ifstream object and then use the open() function to specify the file name and mode. For ofstream, the process is the same, except you use the ofstream object and open the file in write mode.

What happens if you try to open a file that does not exist?

If you try to open a file that does not exist using ifstream or ofstream, an error will occur. You can check for this error using the is_open() function, which will return false if the file does not exist or cannot be opened.

What are some common formatting options available when using ifstream and ofstream?

Some common formatting options include setting the precision and width of the data, using manipulators to control formatting, and using flags to set specific formatting options such as scientific notation or left/right alignment.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
6
Views
885
Replies
10
Views
958
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top