1

Something is definitely wrong with my loop because after reading and executing the first line the programs ends.

if (infile.is_open())
{
    cout << "Input filename: ";
    cin>>filename;
    infile.open(filename.c_str());

    cout<< "Output filename: ";
    cin>>filename;
    outfile.open(filename.c_str());


    while(getline(infile,input))
    {
        string output = "";
        for(int x = 0; x < input.length(); x++)
            output += cipher(input[x]);

        cout<<output<<endl;
        outfile<<output;
    }
}

Any suggestions on how to make this work?

EDIT

Followed the suggestions and got this:

if (infile.is_open()) {

        cout << "Input filename: ";
        cin>>filename;
        infile.open(filename.c_str());
        if (!infile.is_open())
        {
        std::cout << "Failed to open the input file." << std::endl;
        return -1;
        }


        cout<< "Output filename: ";
        cin>>filename;
        outfile.open(ofilename.c_str());
        if (!outfile.is_open())
        {
        std::cout << "Failed to open the output file." << std::endl;
        return -1;
        }


        while(getline(infile,line)){

        string output = "";
        for(int x = 0; x < input.length(); x++) {

        output += cipher(input[x]);


        }

        }

BUT it still reads only the first line...everything else is working perfectly fine....just can't read anything beyond the first line..

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
devodid
  • 13
  • 1
  • 6
  • 1
    `if (infile.is_open()) { ... infile.open(filename.c_str()); ... }` - doesn't make any sense to me. – LihO Mar 20 '13 at 01:52
  • @LihO you're not alone on that opinion. – WhozCraig Mar 20 '13 at 01:52
  • Can you explain why you're testing for `in file.is_open()` before opening `infile`, then promptly ***not*** checking it when you *should* ? – WhozCraig Mar 20 '13 at 02:01
  • Again, after your edit, **why** are you testing for `infile.is_open()` before actually opening the file?? And your edited code cannot possibly produce the same output as the prior code if the files opened successfully, since you're not writing anything to the output file or the console. You're just building a string and throwing it away. – WhozCraig Mar 20 '13 at 02:21
  • Always a brides maid... Ah well. At least it helped. – WhozCraig Mar 21 '13 at 04:14
  • Do not remove the content of your question just because you got an answer, please. – Andrew Barber Oct 25 '13 at 19:29

2 Answers2

1

It seems that you misunderstood the point of the fstream's is_open() method, since this code:

if (infile.is_open())
{
    cout << "Input filename: ";
    cin>>filename;
    infile.open(filename.c_str());
    ...
}

checks whether the infile has been successfully opened (i.e. if either a previous call to member open succeeded or if the object was successfully constructed using the parameterized constructor,
and close has not been called since
) and in case it is open it retrieves the name of the input file from cin and opens the file.

Good start would be the program that reads from the input file line by line and writes these lines to the output file without processing them:

// retrieve the name of the input file and open it:
cout << "Input filename: ";
cin>>filename;
infile.open(filename.c_str());
if (!infile.is_open())
{
    std::cout << "Failed to open the input file." << std::endl;
    return -1;
}

// retrieve the name of the output file and open it:
cout << "Output filename: ";
cin >> filename;
outfile.open(filename.c_str());
if (!outfile.is_open())
{
    std::cout << "Failed to open the output file." << std::endl;
    return -1;
}

std::string line;
while(getline(infile,line))
{
    std::cout << line << std::endl;
    outfile << line;
}
LihO
  • 41,190
  • 11
  • 99
  • 167
0

So I suggest this.

  1. Write char cipher(char ch) to return enciphered input for anything. if you don't want to encipher whitespace, then don't. But always return the enciphered character or unmodifed character.

  2. Use std::transform , std::istream_iterator , and std::ostream_iterator to transform your input and output files.

  3. Check your file states at the correct times.

An example appears below:

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

char cipher(char ch)
{
    if (std::isalpha(ch))
    {
        // TODO: change ch to whatever you want here.
    }

    // but always return it, whether you changed it or not.
    return ch;
}

int main()
{
    int res = EXIT_SUCCESS;

    string in_filename, out_filename;
    cout << "Input filename: ";
    cin >> in_filename;
    cout << "Output filename: ";
    cin >> out_filename;

    // don't skip whitespace
    ifstream infile(in_filename);
    ofstream outfile(out_filename);
    if ((infile >> noskipws) && outfile)
    {
        std::transform(istream_iterator<char>(infile),
                       istream_iterator<char>(),
                       ostream_iterator<char>(outfile),
                       cipher);
    }
    else
    {
        perror("Failed to open files.");
        res = EXIT_FAILURE;
    }
    return res;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141