Outputting to a File Modified Text

This is a discussion on Outputting to a File Modified Text within the C++ Programming forums, part of the General Programming Boards category; I need some help with this program. For some reason, the program is going into an infinite loop. I believe ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Outputting to a File Modified Text

    I need some help with this program. For some reason, the program is going into an infinite loop. I believe the problem to be in the readAndEdit fxn, but I can't see why it's going into it's infinite loop.

    The program is supposed to take input in from an input file line by line into an array. It is supposed to remove consecutive spaces, count the number of words, count the number of sentences. It is then supposed to output the modified text to an output file.

    example output of a line: 3: one two three

    the number of words in the line is to precede the text.

    here's the code:
    Code:
    // Preprocessor Directives
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    // Global Constants
    char PERIOD = '.';
    char EXCLAMATION = '!';
    char QUESTION = '?';
    char BLANK = ' ';
    
    // Function prototypes
    
    // Function name: output_header
    // Description: This function outputs the external header
    // Precondition: none
    // Postcondition: The external header is output
    // Usage: output_header();
    void output_header();
    
    // Function name: openFiles
    // Description: This function opens the input and output files.
    // Precondition: one unloaded ifstream variable and one unloaded
    //               ofstream variable
    // Postcondition: Input file is open and ready for reading; output
    //                file is open and ready for writing
    // Usage: openFiles(x, y);
    void openFiles(ifstream &inFile, ofstream &outFile);
    
    // Function name: readAndEdit
    // Description: This function reads the input from the input file
    //              and outputs edited input to the output file.
    // Precondition: loaded ifstream variable and loaded ofstream
    //               variable
    // Postcondition: An edited version of the input file is in the
    //                output file.
    // Usage: readAndEdit(x, y);
    void readAndEdit(ifstream &inFile, ofstream &outFile);
    
    // Function name: closeFiles
    // Description: This function closes the input file, inlab08.dat
    //              and output file, outlab08.dat.
    // Precondition: loaded ifstream variable and loaded ofstream
    //               variable
    // Postcondition: inlab08.dat and outlab08.dat are closed
    // Usage: closeFiles(x, y);
    void closeFiles(ifstream &inFile, ofstream &outFile);
    
    int main()
    {
       // Variable Declarations
       ifstream inFile;
       ofstream outFile;
       
       // Output external header
       output_header();
       
       // Open input and output files
       openFiles(inFile, outFile);
       
       // Edit input to be placed in output file
       readAndEdit(inFile, outFile);
       
       closeFiles(inFile, outFile);
       
       return 0;
    }
    
    // Function name: output_header
    // Description: This function outputs the external header
    // Precondition: none
    // Postcondition: The external header is output
    // Usage: output_header();
    void output_header()
    {
       cout << endl;
       cout << "This program reads a paragraph from the input file 'inlab8.dat'";
       cout << endl;
       cout << "and outputs an edited version of the text to the output file";
       cout << endl;
       cout << "'outlab8.dat',  The edited version includes the removal of dual";
       cout << endl;
       cout << "spaces and the number of words per line at the beginning of each";
       cout << endl;
       cout << "line.  The output file also contains the number of sentences in";
       cout << endl << "the file." << endl;
       cout << "*****************************************************************";
       cout << endl;
    }
    
    // Function name: openFiles
    // Description: This function opens the input and output files.
    // Precondition: one unloaded ifstream variable and one unloaded
    //               ofstream variable
    // Postcondition: Input file is open and ready for reading; output
    //                file is open and ready for writing
    // Usage: openFiles(x, y);
    void openFiles(ifstream &inFile, ofstream &outFile)
    {
       inFile.open("inlab8.txt");
       if(!inFile) {
          cerr << "Error in opening file 'inlab8.dat'." << endl;
          exit(1);
       }
       
       outFile.open("outlab8.dat");
       if(!outFile) {
          cerr << "Error in opening file 'outlab8.txt'." << endl;
          exit(1);
       }
    }
    
    // Function name: readAndEdit
    // Description: This function reads the input from the input file
    //              and outputs edited input to the output file.
    // Precondition: loaded ifstream variable and loaded ofstream
    //               variable
    // Postcondition: An edited version of the input file is in the
    //                output file.
    // Usage: readAndEdit(x, y);
    void readAndEdit(ifstream &inFile, ofstream &outFile)
    {
       int wordCount = 0, sentenceCount = 0, i = 0, j = 0;
       char lineArray[80], outArray[80];
       char tempChar, lastChar;
       
       inFile.setf(ios::adjustfield | ios::right);
       
       inFile.get(tempChar);
       
       while(tempChar != EOF) {
          i = 0;
          while(i < 70) {
             lineArray[i++] = tempChar;
             inFile.get(tempChar);
          }
          for(j = 0; j < i; j++) {
             outArray[j] = lineArray[j];
             lastChar = lineArray[j];
             if(lastChar == BLANK) {
                wordCount++;
                if(lineArray[j + 1] == lastChar) {
                   j++;
                }
             }
             if(lastChar == PERIOD || lastChar == EXCLAMATION || lastChar == QUESTION) {
                sentenceCount++;
             }
          }
          outFile << setw(3) << wordCount + 1 << ": ";
          for(j = 0; j < i; j++) {
             outFile.put(outArray[j]);
          }
          inFile.get(tempChar);
       }
       outFile << sentenceCount + 1;
    }
    
    // Function name: closeFiles
    // Description: This function closes the input file, inlab08.dat
    //              and output file, outlab08.dat.
    // Precondition: loaded ifstream variable and loaded ofstream
    //               variable
    // Postcondition: inlab08.dat and outlab08.dat are closed
    // Usage: closeFiles(x, y);
    void closeFiles(ifstream &inFile, ofstream &outFile)
    {
       inFile.close();
       outFile.close();
    }
    Also, an example input file is attached.
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > while(tempChar != EOF)
    tempChar is a char, and EOF is an int
    My guess is your real EOF is being cast to a char (say 255), and therefore can never be equal to the real EOF (probably -1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    ifstream::get(char&) does not put EOF (or anything for that matter) in the given parameter when the stream is at EOF.

    You can fix the infinite loop with "while(!inFile.eof())", but you still have some more bugs to exterminate.

    gg

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Ok, I fixed the loop issues. Now, I'm having trouble with it outputting correctly. I changed the readAndEdit fxn to:
    Code:
    void readAndEdit(ifstream &inFile, ofstream &outFile)
    {
       int wordCount = 0, sentenceCount = 0, i = 0, j = 0, k;
       char lineArray[80], outArray[80], tempArray[80];
       char tempChar, lastChar;
       
       inFile.setf(ios::adjustfield | ios::right);
       
       inFile.get(tempChar);
       
       while(!inFile.eof()) {
          i = 0;
          while(i < 71) {
             lineArray[i++] = tempChar;
             inFile.get(tempChar);
          }
          
          for(j = 0; j < i; j++) {
             tempArray[j] = lineArray[j];
          }
          
          for(j = 0; j < i; j++) {
             if(tempArray[j] == BLANK && tempArray[j + 1] == BLANK) {
               for(k = j; k + 1 < i; k++) {
                   tempArray[k] = tempArray[k + 1];
                }
             }
          }
          
          for(j = 0; j < i; j++) {
             if(tempArray[j] == BLANK) {
                wordCount++;
             } else if(tempArray[j] == PERIOD || tempArray[j] == EXCLAMATION 
                || tempArray[j] == QUESTION) {
                   sentenceCount++;
             }
          }
          
          for(j = 0; j < i; j++) {
             outArray[j] = tempArray[j];
          }
           
          outFile << setw(3) << wordCount << ": ";
          
          for(j = 0; j < i; j++) {
             outFile.put(outArray[j]);
          }
       }
       outFile << sentenceCount;
    }
    This is the output I'm getting:
    Code:
     13: This is a test of the lab eight
    program. That extra space should be ree 25: moved from the line!
    There should be four lines to this output file. TT 31: he last input
    number should read four sentences?
    // a few lines later
    4
    The input is as follows;
    Code:
    This is a test of the lab eight
    program.  That extra space should be removed from the line!
    There should be four lines to this  output file. The last  input
    number should  read four sentences?
    This is what is in the attached input file.

    It is supposed to output:
    Code:
     8: This is a test of the lab eight
    10: program. That extra space should be removed from the line!
    12: There should be four lines to this output file. The last input
     5: number should read four sentences?
    4
    I can't see where the problem is in the code right now.

    Thank you.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Your code is written with the assumption that every line will be exactly 70 characters.
    The first line of your own inlab8.txt only contains 32 characters.

    You need to process the data a line at a time, not 70 characters at a time.

    gg

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I'm not allowed to process the file a line at a time. If I could, I would have already done so. I need a way to do it while processing it a char at a time. I might just do it a line at a time though.

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    Originally posted by alpha
    I'm not allowed to process the file a line at a time. If I could, I would have already done so. I need a way to do it while processing it a char at a time. I might just do it a line at a time though.
    what do you mean not allowed?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Code:
    void my_getline(istream &in, char *buff)
    {
        // Put code here, then use it
    }
    gg

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    As Codeplug is suggesting, instead of this:
    Code:
          i = 0;
          while(i < 71) {
             lineArray[i++] = tempChar;
             inFile.get(tempChar);
          }
    Have something like this, which you could make into a function:
    Code:
          i = 0;
          while(!inFile.eof() && (tempChar = inFile.get()) != '\n') {
             lineArray[i++] = tempChar;
          }
    Or this:
    Code:
          i = 0;
          while(!inFile.eof() && (lineArray[i++] = inFile.get()) != '\n') {
          }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM

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