1

我的循环肯定有问题,因为在读取并执行第一行之后程序结束。

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;
    }
}

关于如何使这项工作的任何建议?

编辑

遵循了建议并得到了这个:

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]);


        }

        }

但它仍然只读取第一行......其他一切都很好......只是无法读取第一行以外的任何内容......

4

2 回答 2

1

看来您误解了fstream'is_open()方法的要点,因为这段代码:

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

检查是否infile已成功打开(即,如果之前对成员的调用open成功,或者是否使用参数化构造函数成功构造了对象,
并且从那时起没有调用 close
),如果它打开,则检索输入的名称文件cin并打开文件。

好的开始是程序逐行读取输入文件并将这些行写入输出文件而不处理它们:

// 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;
}
于 2013-03-20T02:05:15.793 回答
0

所以我建议这个。

  1. 写入以返回任何内容char cipher(char ch)的加密输入。如果您不想加密空格,请不要。但总是返回加密字符或未修改字符。

  2. 使用std::transformstd::istream_iteratorstd::ostream_iterator来转换您的输入和输出文件。

  3. 在正确的时间检查您的文件状态。

一个例子如下所示:

#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;
}
于 2013-03-20T02:28:09.943 回答