Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Format text(ifstream, ofstream)

  1. Mar 31, 2009 #1
    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 (Text):

    #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. jcsd
  3. Mar 31, 2009 #2

    jtbell

    User Avatar

    Staff: Mentor

    What does it actually look like when you run the program?
     
  4. Mar 31, 2009 #3
    No what im 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...
     
  5. Apr 1, 2009 #4

    jtbell

    User Avatar

    Staff: Mentor

    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?
     
  6. Apr 1, 2009 #5
    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.
     
  7. Apr 1, 2009 #6

    jtbell

    User Avatar

    Staff: Mentor

    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?
     
  8. Apr 2, 2009 #7
    Code (Text):
      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
     
  9. Apr 2, 2009 #8

    jtbell

    User Avatar

    Staff: Mentor

    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.
     
  10. Apr 2, 2009 #9
    How do i do that?
    everytime i try to do it i get an error
     
  11. Apr 2, 2009 #10

    jtbell

    User Avatar

    Staff: Mentor

    Can you show us what you tried, and what the error message was?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Format text(ifstream, ofstream)
  1. Ifstream issues (Replies: 8)

Loading...