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

标签 c++ fstream file-handling

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

请告诉我问题出在哪里,我是如何解决的?

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

with out loop working fine >>

 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

最佳答案

您有一大堆问题,其中最重要的是 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];

如果您需要类的多个实例,请使用类 vector 或类数组。但是,您的代码中都不需要。由于您通过 do { ... } while (..); 循环在每次迭代中创建了 File_Opening_and_Closing 的新实例,因此您可以简单地使用:

    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 */} 每个输入都需要进行类似的测试,以确保您可以捕获用户生成手动 EOF< 的输入取消 使用 Ctrl+d(或 Windows 上的 Ctrl+z)。

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

让您添加验证,解决其他问题,您可以执行类似的操作:

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

如果您还有其他问题,请告诉我。

关于c++ - getline() 函数在 while 循环中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55233456/

相关文章:

c++ - std::ofstream 打开文件并在特定偏移量中替换

c++ - 在 C++ 中检测文件中的空间

python - 在python中读取.dat文件

c - 从文件中动态读取结构时 fread 不工作

c++ - 使用 C++ 模板时出现错误 C2955 和错误 C2244

c++ - 错误 : type 'std::__1::basic_string<char>' does not provide a call operator

C++,在尝试创建类 : error: no ‘void media::*()’ member function declared in class ‘media’ 时收到此消息

c++ - 无法从具有字符串成员的结构 vector 中读取名称

c++ - 编辑开源库

c++ - Fstream _Fgetc 访问冲突