I'm trying to accomplish the very simple task of storing text from a file into a c-string. For some reason, it's just not working.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct student
{
char id[8];
char responses[20];
int score;
double grade;
};
int main()
{
ifstream inFile("Ch8_Ex6Data.txt");
char answerKey[20];
inFile.getline(answerKey, 20);
student students[256];
inFile.getline(students[0].id, 8);
cout << answerKey << endl;
cout << students[0].id;
return 0;
}
Here's a copy of Ch8_Ex6Data.txt
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
answerkey
works exactly the way it's supposed to, but student[0].id
remains blank after the get function. I've tried using the extraction operator >>
, getline()
, and get()
, but none of them actually work. What am I doing wrong?
Edit for clarity: I want ABC54102
to be stored in student[0].id
.