J_Ray93
J_Ray93

Reputation: 9

Issue with saving an edited infile to an outfile in c++

Im having an issue with the last section of coding on here. The // Copy files from infile to outfile. The program transfers my infile which is simply a an 8 digit number , 20392207,splits it up into individual digits using the .at method; and is supposed to save that output to an outfile. I cant figure out how to save the output to the outfile. Any advice?

infile looks as follows

20392207

program output looks like this

The input number :20392207
The number 1:2
The number 2:0
The number 3:3
The number 4:9
The number 5:2
The number 6:2
The number 7:0
The number 8:7

outfile is supposed to look like the program out put, but instead just looks like an exact copy of the infile.

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<cmath>
using namespace std;
int main()
{
    string ifilename, ofilename, line, line2;
    ifstream inFile, checkOutFile;
    ofstream outFile;
    char response;
    int i;


    // Input file
    cout << "Please enter the name of the file you wish to open : ";
    cin >> ifilename;
    inFile.open(ifilename.c_str());
    if (inFile.fail())
    {
        cout << "The file " << ifilename << " was not successfully opened." << endl;
        cout << "Please check the path and name of the file. " << endl;
        exit(1);
    }
    else
    {
        cout << "The file is successfully opened." << endl;
    }


    // Output file
    cout << "Please enter the name of the file you wish to write : ";
    cin >> ofilename;
    checkOutFile.open(ofilename.c_str());
    if (!checkOutFile.fail())
    {
        cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite it? (y/n) : ";
        cin >> response;
        if (tolower(response) == 'n')
        {
            cout << "The existing file will not be overwritten. " << endl;
            exit(1);
        }
    }
    outFile.open(ofilename.c_str());
    if (outFile.fail())
    {
        cout << "The file " << ofilename << " was not successfully opened." << endl;
        cout << "Please check the path and name of the file. " << endl;
        exit(1);
    }
    else
    {
        cout << "The file is successfully opened." << endl;
    }



    // Copy file contents from inFile to outFile
    while (getline(inFile, line))
    {
    cout << "The input number :" << line << endl;
        for (i = 0; i < 8; i++)
        {
            cout << "The number " << i + 1 << ":";
            cout << line.at(i);
            cout << endl;
        }

    outFile << line << endl;    
    }



    // Close files
    inFile.close();
    outFile.close();
} // main

Upvotes: 0

Views: 233

Answers (2)

maxlazar
maxlazar

Reputation: 170

You need to write to outFile inside the while(getline(inFile, line)) loop.

[edit] see user4581301's answer for a more thorough treatment.

Upvotes: 0

user4581301
user4581301

Reputation: 33982

Here we can see that outFile is only written to outside of the while loop:

while (getline(inFile, line))
{
    cout << "The input number :" << line << endl;
    for (i = 0; i < 8; i++)
    {
        cout << "The number " << i + 1 << ":";
        cout << line.at(i);
        cout << endl;
    }
}
outFile << line << endl;

It has no chance of containing the same output as the console

Solution: Write inside the loop the same stuff that was written to the console:

while (getline(inFile, line))
{
    cout << "The input number :" << line << endl;
    outFile << "The input number :" << line << endl;
    blah blah blah
}

But this looks like crap and a function makes like a better solution by eliminating duplication and upping re-usability.

void output(std::ostream & out,
            const std::string & line)
{
    out << "The input number :" << line << endl;
    for (int i = 0; i < 8; i++)
    {
        out << "The number " << i + 1 << ":";
        out << line.at(i);
        out << endl;
    }
}

and called:

while (getline(inFile, line))
{
    output(cout, line);
    output(outFile, line);
}

Upvotes: 1

Related Questions