mercredi 31 juillet 2019

Reading and writing to files in C++

I have an assignment. I have to read in a text file. The file can contain alpha characters, numbers or any other non alphanumeric, including whitespace etc. Once I've read the file, I have to make the 1st alpha character uppercase and make the remaining contents correct for uppercase and lowercase. Then I have to write the corrected contents to a different file.

I've nearly finished it, but am stuck on the last part. If the last character on the read file is a period then the program doesn't finish and is stuck writing endless periods to the write file. If the last character of the input file is not a period, then the program finishes but replicates that last character twice in the write file.

I feel I need to do something with !inFile.eof() but nothing I've tried on that so far has worked. Appreciate any guidance.

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;


int main() {

    ifstream inFile;
    ofstream outFile;
    char ch;
    string inputFileName;
    string outputFileName;

    cout << "Please enter the name and extension of the file you want read from: " << endl;
    cin >> inputFileName;

    cout << "Please enter the name and extension of the file you want to write to: " << endl;
    cin >> outputFileName;

    inFile.open(inputFileName, ios::in);
    outFile.open(outputFileName, ios::out);
    if (!outFile) {
        cerr << "Unable to open: " << outputFileName << endl;
        exit(1);
    }

    if (inFile) {
        // The following if statements take care of uppercasing the first alpha character. Do this before the main while loop starts
        if (!inFile.eof()) {
            inFile.get(ch); //get the 1st character
            if ((!islower(ch)) && (!isupper(ch))) { //if it's not an alpha enter into a "not alpha" while loop
                while ((!islower(ch)) && (!isupper(ch))) {
                    outFile.put(ch); //just write non alpha characters to output file as-is
                    inFile.get(ch); //get the next character and keep looping until it's an alpha
                }
                if (isupper(ch)) {
                    outFile.put(ch); //if the first alpha character is uppercase, write to outFile as is
                }
                else {
                    outFile.put(toupper(ch)); //if the 1st alpha is lowercase, write to outFile as uppercase
                }
            }
        }

        while (inFile)  //move on to the rest of the document
        {
            if (!inFile.eof()) {
                inFile.get(ch);
                if (ch == '.') { //if ch is a period, take action to get the next alpha to uppercase
                    outFile.put(ch); // write the period to the outFile
                    inFile.get(ch); //get the next char
                    if ((!islower(ch)) && (!isupper(ch))) { // if the char after period is not an alpha
                        while ((!islower(ch)) && (!isupper(ch))) { //loop while it is not an alpha
                            outFile.put(ch); //just write it to the outfile
                            inFile.get(ch); //get the next char
                        }
                            if (isupper(ch)) {
                                outFile.put(ch); // write the uppercase back to the output file
                            }
                            else if (islower(ch)) {
                                outFile.put(toupper(ch)); //write the lowercase back as an uppercase
                            }
                            else {
                                outFile.put(ch);
                            }
                    }
                    else if (islower(ch)) {
                        outFile.put(toupper(ch)); // write the uppercase back to the output file
                    }
                    else {
                        outFile.put(ch);
                    }
                }
                else if (isupper(ch)) { // change non sentence starting uppercases to lowercase 
                    outFile.put(tolower(ch));
                }
                else {
                    outFile.put(ch);
                }
            }
        }
    }
    else {
        cerr << "Unable to open file: " << inputFileName << endl;
        exit(1);
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire