Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';在文件处理中处理字符串时,无法从用户处获取输入_C++_String_File Handling - Fatal编程技术网

C++ Can';在文件处理中处理字符串时,无法从用户处获取输入

C++ Can';在文件处理中处理字符串时,无法从用户处获取输入,c++,string,file-handling,C++,String,File Handling,我不知道该怎么处理, 每当我运行此代码时,我都无法在运行时输入字符串。编译器只是跳过了该部分。 事实上,当我删除文件处理时,字符串工作得很好,但是当我添加文件处理时,编译器会跳过字符串,当我使用cin.ignore():时,我可以在运行时输入字符串,但输出完全混乱。 我是个编程新手,需要帮助。 谢谢:) 帮我进来 输入值 //创建文件的流。 //ifstream从文件中读取数据。 #包括 #包括 #包括 无效写入(); 无效读取(); 使用名称空间std; int main(){ CUT你真的写

我不知道该怎么处理, 每当我运行此代码时,我都无法在运行时输入字符串。编译器只是跳过了该部分。 事实上,当我删除文件处理时,字符串工作得很好,但是当我添加文件处理时,编译器会跳过字符串,当我使用cin.ignore():时,我可以在运行时输入字符串,但输出完全混乱。 我是个编程新手,需要帮助。 谢谢:) 帮我进来

输入值

//创建文件的流。
//ifstream从文件中读取数据。
#包括
#包括
#包括
无效写入();
无效读取();
使用名称空间std;
int main(){

CUT你真的写了这么多代码,在尝试测试它是否正常工作之前,很可能有多个问题,这使得很难找出每一个问题。最好只写几行,然后测试它们,确保它们在写更多之前正确工作。这就是专业C++。开发人员编写大型应用程序。当然,他们不会一次编写所有内容。这样,一次只需几行新代码,如果有问题,它只是一个问题,很容易识别和修复。@SamVarshavchik抱歉,我只是共享了整段代码,而不是共享了有问题的部分。因此,现在我进入了代码创建阶段下面是错误!请检查其中的缺陷。“#include#include#include#include使用名称空间std;int main(){char teacher[20],student[20];无法猜测您的文件从未打开过。请使用一个
/
。您还可以将循环从
while(!myfile.eof()){…}
更改为
while(myfile>>teacher>>student){ ... }
@Scheff它今天在Windows 10上也对我起了作用。我肯定我以前见过它失败,因为我记得我修复过它。如果我在做一项作业,在那里我被评分,我会尽最大努力确保这些小事情是正确的,因为这是一个非常低的努力改变。@RetiredInja我曾经注意到这一点,甚至不记得了ber不再依赖于哪个操作系统。我不会依赖它,当然,在我的生产代码中,我也不会容忍这种情况。:-)
    //ofstream to create file.
//ifstream to read data from file.
#include <iostream>
#include <fstream>
#include <string>
void write();
void read();
using namespace std;
int main() {
    cout << "Press 1 to write data into file" << endl;
    cout << "Press 2 to read data from file" << endl;
    int n;
    cin >> n;
    if (n == 1) {
        write();
    }
    else if (n == 2) {
        read();
    }
    else {
        return 0;
    }
    
    system("pause");
}
void write() {
        string teacher, student;
cout << "Enter teacher's name: ";
getline(cin, teacher);
cout << teacher << endl;
cin.ignore();
cout << "Enter student's name: ";
getline(cin, student);
cout << student << endl;
cin.ignore();

    ofstream myfile;
    myfile.open("F://abc.txt", ios::app);
    myfile << teacher << "...." << student << endl;
    myfile.close();

}

void read() {
    string teacher, student;
    ifstream myfile;
    myfile.open("F://abc.txt");

    while (!myfile.eof())
    {
        myfile >> teacher >> student;

        cout << teacher << " " << student << endl;
    }

    myfile.close();
}