ifstream getline问题(它只读取第一行)

时间:2013-03-20 01:47:08

标签: c++ loops getline

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

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


        }

        }

但它仍然只读取第一行...其他一切工作正常......只是读不到第一行以外的任何东西..

2 个答案:

答案 0 :(得分:1)

您似乎误解了fstream的{​​{3}}方法的要点,因为此代码:

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

答案 1 :(得分:0)

所以我建议这样做。

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

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

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

  4. 以下示例如下:

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