C++ 文件I/O未读入任何内容

C++ 文件I/O未读入任何内容,c++,file-io,C++,File Io,我在读写文本文件的内容时遇到问题。 我试着分别阅读问题、答案和错误答案,但我没有得到任何东西来阅读 这是我的密码: #include "Question.h" #include <vector> #include <fstream> #include <iostream> using namespace std; Question::Question() { this->m_question = ""; this->m_questi

我在读写文本文件的内容时遇到问题。 我试着分别阅读问题、答案和错误答案,但我没有得到任何东西来阅读

这是我的密码:

#include "Question.h"
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
Question::Question()
{
    this->m_question = "";
    this->m_question = "";
    this->m_wrongAns1 = "";
    this->m_wrongAns2 = "";
    this->m_wrongAns3 = "";
    char trash[256];
    char value[256];
    int count;
}
Question::Question(string p_question, string p_answer, string p_wrongAns1, string p_wrongAns2, string p_wrongAns3)
{
    this->m_question = p_question;
    this->m_question = p_answer;
    this->m_wrongAns1 = p_wrongAns1;
    this->m_wrongAns2 = p_wrongAns2;
    this->m_wrongAns3 = p_wrongAns3;
    char trash[256];
    char value[256];
    int count;
}
string Question::getQuestion(string p_filename)
{
    ifstream myfile(p_filename);
    char trash[256];
    char value[256];
    myfile.getline(trash, 256);     //Linebreak
    myfile.getline(trash, 256);     //Name tag
    myfile.getline(value, 256);     //Name
    m_question.assign(value);
    cout << m_question;
    return m_question;
}
string Question::getAnswer(string p_filename)
{
    return "";
}
vector<string> Question::getWrongAnswers(string p_filename)
{
    vector<string> questionList;
    vector <string> ::iterator questionIt;
    return questionList;
}
大体上,我只做了一个简单的调用:

 getQuestions("Questions\\Questions.txt";

更改这行代码

ifstream myfile(p_filename);
对此

ifstream myfile(p_filename);
if (!myfile.is_open())
    cerr << "could not open file\n";
ifstream myfile(p_文件名);
如果(!myfile.is_open())

cerr一个好的开始是看看哪些操作失败了。根据代码,我猜,它无法打开文件,但您可以检查它是否打开:
如果(!myfile){std::cout奇怪的代码,为什么会有
字符垃圾[256];字符值[256];int count;
在两个构造函数的末尾?噢,我不知道我在想什么,我应该把它取出来,lolYe,它无法打开文件,这意味着什么?文件无法打开的原因有很多,但最有可能的是,您的程序正在另一个位置查找它,而不是它真正所在的位置。您可以这里是
“Questions\\Questions.txt”
,你认为这意味着什么?是什么让你相信你的程序理解你的意思?
“Questions\\Questions.txt”是一个相对路径,但它是什么?你知道吗?或者你知道你知道的。你最好使用绝对路径。这是一个困难的原因,就是C++程序如何发现一个文件与C++语言无关。它与你的操作系统(无论它是什么)以及你如何运行程序有关。(无论如何)。所以除非你提供更多的细节,否则很难给出具体的建议。整理好了吗?正确吗?我把它移到了.exe旁边,它成功了:)
ifstream myfile(p_filename);
if (!myfile.is_open())
    cerr << "could not open file\n";