这是一项作业,所以我遵循所需结构布局的指定准则以及重载。试图让我的程序读取每一行并将每一行分配给变量。
文本文件如下:
John
S
Maggey
G
我的代码如下:
#include "iostream"
#include "fstream"
namespace
{
char buffer[1024];
int allocated = 0;
}
//====================================================================
struct student
{
char *firstname;
char lastname;
int studentId;
int occupied;
student() : lastname(0), studentId(0), occupied(0)
{
firstname = new char[64];
for (int i = 0; i < 64; ++i)
firstname[i] = 0;
}
student(int s)
{
std::cout << "constructor" << std::endl;
std::cout << "Allocated: " << allocated << std::endl;
int currentLoc = allocated;
allocated += s;
firstname = new (&buffer[currentLoc]) char[s];
for (int i = 0; i < 64; ++i)
firstname[i] = 0;
}
void *operator new(size_t s)
{
std::cout << "Operator new allocated: " << allocated << std::endl;
int currentLoc = allocated;
allocated += s;
return &buffer[currentLoc];
}
void student::operator delete(void *ptr)
{
std::cout << "Delete called " << std::endl;
std::free(ptr);
}
student::~student()
{
}
};
//====================================================================
int main(int argc, char** argv)
{
student *studentLoader = new student[25];
std::fstream fin;
fin.open("students.txt");
char ln;
for (int i = 0; i < 25; ++i)
{
fin.getline(studentLoader[i].firstname, 99);
fin.getline(ln, 64);
studentLoader[i].lastname = ln;
studentLoader[i].studentId = (rand() % (9999 - 999)) + 999;
studentLoader[i].occupied = 1;
if ((i % 10) == 0)
{
std::cout << "First name: " << studentLoader[i].firstname << " Last initial: " << studentLoader[i].lastname << " ID: " << studentLoader[i].studentId << std::endl;
delete[] studentLoader;
}
}
fin.close();
return 0;
}
我也尝试过:
std::fstream fin;
fin.open("students.txt");
char ln;
for (int i = 0; i < 25; ++i)
{
fin.getline(studentLoader[i].firstname, 99);
fin.getline(studentLoader[i].lastname, 99);
studentLoader[i].lastname = ln;
studentLoader[i].studentId = (rand() % (9999 - 999)) + 999;
studentLoader[i].occupied = 1;
if ((i % 10) == 0)
{
std::cout << "First name: " << studentLoader[i].firstname << " Last initial: " << studentLoader[i].lastname << " ID: " << studentLoader[i].studentId << std::endl;
delete[] studentLoader;
}
}
fin.close();
return 0;
}
但是第二次尝试getline会收到错误。
尝试时
fin >> studentLoader[i].firstname;
不会在变量中插入任何内容。
答案 0 :(得分:0)
如果这不是拼写错误并且您真的想要为lastname
使用单个字符,那么您应该将其作为单个字符阅读:
fin >> studentLoader[i].lastname >> std::ws
。
另请考虑阅读documentation了解您正在使用的函数和类。这通常是个好习惯,可以帮助你避免相当多的失败。
答案 1 :(得分:0)
通过将struct student
和firstname
的类型更改为lastname
来稍微修改std::string
,您不必动态分配/取消分配内存,您将减少代码减半。
例如,您可以将其定义为:
struct student {
// constructor
student (std::stirng fn, std::stirng ln, int ID, int oc)
: firstname(fn), lastname(ln), studentID(ID), occupied(oc) { }
// data members
std::string firstname;
std::string lastname;
int studentID;
int occupied;
};
然后从struct student
中的文件和商店中读取,您可以使用std::vector
,如下所示:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main () {
// stores all elements created from the reading of the file
std::vector<student> class_of_students;
// attach an input stream
std::ifstream fin("students.txt");
// check if file successfully opened
if (!fin) std::cerr << "Can't open input file!\n";
// line of text
std::string line;
// read file line by line
while (getline(fin, line)) {
// stream to extract data from a line
std::stringstream ss(line);
// input variables
std::string first_name;
std::string last_name;
int ID;
int occupied;
// example that assumes line format: "f_name l_name ID occ"
while (ss >> first_name >> last_name >> ID >> occupied) {
// store one element of type student to the vector
class_of_students.emplace_back(student(first_name, last_name, ID, occupied));
}
}
}
有关使用std::string和std::vector的更多信息。
此外,当您生成学生ID
时,您需要它不仅是随机的,而且也是唯一的。