getline()函数在while循环中不起作用

问题描述 投票:0回答:1

我想用一个外部类“File_Opening_and_Closing.cpp”创建一个简单的文件输入输出程序,当我使用“getline()”函数时没有任何循环它工作正常但是当我使用“getline()”函数和do while循环时“ main.cpp“它粉碎了。

请告诉我问题出在哪里以及如何解决?

main.cpp中

    #include<iostream>
    #include<fstream>
    #include "File_Opening_and_Closing.h" using namespace std;

    int main() {
        int i=1;
        char file_make='y';
        char file_insert='y';

        do
        {
            File_Opening_and_Closing file[i];

            do
            {
                file[i].put_data();
                file[i].show_data();
                cout<<"Do you want to insert text again ? 'y' OR 'n'"<<endl;
                cin>>file_insert;
            }while(file_make=='y');
            i++;
            cout<<"Do you want to Make another file ? 'y' OR 'n'"<<endl;
            cin>>file_make;
        }while(file_insert=='y');

return 0;}

用循环工作正常>>

 int main() {
            File_Opening_and_Closing file;
            file.put_data();
            file.show_data();
 return 0;}

File_Opening_and_Closing.cpp

#include "File_Opening_and_Closing.h"

File_Opening_and_Closing::File_Opening_and_Closing()
{
    cout<<"Enter the file name and type => ";
    cin>>file_name;
}

void File_Opening_and_Closing::put_data(){
    ofstream file_out;
    file_out.open(file_name);

    cin.ignore();
    cout<<"Enter the string => ";
    cin.ignore();

//  getline is not working here!

    getline(cin,data);

    data = "Hello World!";

    file_out<<data;
    file_out.close();
}

void File_Opening_and_Closing::show_data(){
    ifstream file_in;

    file_in.open(file_name);

    getline(file_in,data);
    cout<<data;

    file_in.close();
}

File_Opening_and_Closing.h

#ifndef FILE_OPENING_AND_CLOSING_H
#define FILE_OPENING_AND_CLOSING_H
#include<iostream>
#include<fstream>
using namespace std;


class File_Opening_and_Closing
{
    private:
        string file_name;
        string data;
    public:
        File_Opening_and_Closing();
        void put_data();
        void show_data();
    protected:
};

#endif // FILE_OPENING_AND_CLOSING_H

Problem picture

c++ fstream file-handling
1个回答
1
投票

你有很多问题,尤其是How to debug small programs问题,你混淆了用于测试两个do .. while (...);循环的变量,例如:

            ...
            cin>>file_insert;
        }while(file_make=='y');
        // i++; (not needed and VLA's not allowed in C++
        cout<<"Do you want to Make another file ? 'y' OR 'n'"<<endl;
        cin>>file_make;
    }while(file_insert=='y');

其次,如评论中所述,标准C ++不允许使用可变长度数组。

    File_Opening_and_Closing file[i];

如果您需要多于1个类的实例,请使用类向量或类数组。但是,您的代码中也没有必要。由于您通过File_Opening_and_Closing循环每次迭代创建一个新的do { ... } while (..);实例,您可以简单地使用:

    File_Opening_and_Closing file;

通过将file_makefile_insert声明为char类型,你正在为自己做出令人难以置信的努力。相反,只需使它们std::string并测试,例如, if (file_make == "y")。这将允许您使用getline读取所有输入,避免使用混合std::cingetline的问题。

剩下的问题是完全无法验证文件开头,例如, if (!file_out.is_open()) { /* handle error */ }需要对每个输入进行类似的测试,以确保您可以通过Ctrl + d(或Windows上的Ctrl + z)生成手动EOF来捕获输入取消。

另外,避免将using namespace std;放在头文件中。没有必要使用标题将标准命名空间拉入每个文件中(尽管你确实做得很好并且使用标题保护FILE_OPENING_AND_CLOSING_H防止多个包含)。事实上,这里根本没有using namespace std;的理由。只需将std::命名空间解析运算符用于cin, cout等。

留下验证给你添加,解决其他问题,你可以做类似的事情:

File_Opening_and_Closing.h

#ifndef FILE_OPENING_AND_CLOSING_H
#define FILE_OPENING_AND_CLOSING_H

#include<iostream>
#include<fstream>

class File_Opening_and_Closing
{
    private:
        std::string file_name;
        std::string data;
    public:
        File_Opening_and_Closing();
        void put_data();
        void show_data();
    protected:
};

#endif

File_Opening_and_Closing.cpp

#include "File_Opening_and_Closing.h"

File_Opening_and_Closing::File_Opening_and_Closing()
{
    std::cout << "Enter the file name and type => ";
    getline (std::cin, file_name);
}

void File_Opening_and_Closing::put_data(){
    std::ofstream file_out;
    file_out.open(file_name);

    std::cout<<"Enter the string => ";

    getline (std::cin, data);


    file_out << data << '\n';
    file_out.close();
}

void File_Opening_and_Closing::show_data(){
    std::ifstream file_in;

    file_in.open (file_name);

    getline (file_in,data);
    std::cout << data << '\n';

    file_in.close();
}

main.cpp中

#include<iostream>
#include<fstream>

#include "File_Opening_and_Closing.h"

int main (void) {

    std::string file_make = "y";
    std::string file_insert = "y";

    do
    {
        File_Opening_and_Closing file;

        do
        {
            file.put_data();
            file.show_data();
            std::cout << "Do you want to insert text again ? 'y' OR 'n'\n";
            getline (std::cin, file_insert);
        } while (file_insert == "y");
        std::cout << "Do you want to Make another file ? 'y' OR 'n'\n";
        getline (std::cin, file_make);
    } while (file_make == "y");

    return 0;
}

它现在可以根据需要创建任意数量的文件(但如果要添加多个字符串,则需要查看std::ios::app模式。

示例使用/输出

$ ./bin/main
Enter the file name and type => out1
Enter the string => foobar
foobar
Do you want to insert text again ? 'y' OR 'n'
y
Enter the string => barbaz
barbaz
Do you want to insert text again ? 'y' OR 'n'
n
Do you want to Make another file ? 'y' OR 'n'
y
Enter the file name and type => out2
Enter the string => bazbuz
bazbuz
Do you want to insert text again ? 'y' OR 'n'
n
Do you want to Make another file ? 'y' OR 'n'
n

产生的输出文件

$ cat out1
barbaz

$ cat out2
bazbuz

如果您有其他问题,请与我们联系。

© www.soinside.com 2019 - 2024. All rights reserved.