文件I/O没有读取任何内容
File I/O not reading in anything
我在读取和写入文本文件的内容时遇到问题。我试着分别阅读问题、答案和错误答案,但我没有什么可读的。
这是我的代码:
#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;
}
这应该是按行读取并将值分配给一个变量,垃圾就在左边。
问题\问题.txt
Which of the following is NOT a type of virtual collaboration:
Skype
igoogle documents
Hand-written letter Answer
Email
Which of the following are types of CMC?
Video
Instant Messengers Answer
Phone
BlueJ
总的来说,我只做了一个简单的调用:
getQuestions("Questions\Questions.txt";
更改这行代码
ifstream myfile(p_filename);
到这个
ifstream myfile(p_filename);
if (!myfile.is_open())
cerr << "could not open filen";
看看会发生什么。
几乎可以肯定的是,你的代码失败的原因是你没有打开文件,所以先测试一下这个理论。
相关文章:
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 为什么ifstream没有读取任何内容
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- C++使用用户名/SID 或备用读取任何窗口用户 HKEY
- C++ 中是否有任何内置阶乘函数?
- 无法使用 QSerialPort 读取任何内容
- 读取任何与PC兼容的赛车方向盘输入
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 套接字记录未读取任何消息
- 在 C++ 中立即设置故障位(在读取任何字节之前)
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- 文件I/O没有读取任何内容
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 逐字节读取任何文件(不是.bin文件)
- C++ std::istream readsome不读取任何内容
- boost::asio::ip::tcp::socket 不读取任何内容
- basic_ifstream<...>::read() 不读取任何内容
- 在VC++2010中执行任何内置的ARRAY函数