Becky O Brien
Becky O Brien

Reputation: 69

File I/O not reading in anything

I'm having a problem reading in and writing out the contents of the text file. I'm trying to read in questions, answers, and wrong answers separately but I'm not getting anything to read.

Here is my code:

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

This is supposed to read in by the line and assign the value to a variable and the trash is just left.

Questions\Questions.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

In main I just do a simple call of:

 getQuestions("Questions\\Questions.txt";

Upvotes: 0

Views: 51

Answers (1)

john
john

Reputation: 88027

Change this line of code

ifstream myfile(p_filename);

to this

ifstream myfile(p_filename);
if (!myfile.is_open())
    cerr << "could not open file\n";

and see what happens.

Almost certainly the reason your code fails is that you fail to open the file, so test that theory first.

Upvotes: 1

Related Questions