无法读取文件并存储在结构的数据成员中
Can't read file and store in data members of struct
这是针对赋值的,所以我遵循所需结构布局和重载的赋值指南。试图让我的程序读取每一行并将每一行分配给变量。
文本文件如下所示:
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;
}
但是第二次尝试获取线路时会收到错误。
尝试时
fin >> studentLoader[i].firstname;
不会将任何内容插入到变量中。
如果这不是一个错别字,并且你真的想在lastname
中使用单个字符,你也应该把它作为一个字符来阅读: fin >> studentLoader[i].lastname >> std::ws
.
此外,请考虑阅读您正在使用的函数和类的文档。这通常是个好习惯,可以使您免于大量失败。
通过将firstname
和lastname
的类型更改为std::string
来稍微修改您的struct student
,您不必动态分配/释放内存,您将代码减少一半。
例如,您可以将其定义为:
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
时,您不仅需要它是随机的,而且需要唯一的。
相关文章:
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 从 XML 中读取未指定结构的每个数据成员
- 如何将结构数据成员传递到函数中
- 单独构造结构的数据成员合法吗
- 用于向公众公开数据成员的帮助程序结构
- 通过指针算法访问结构数据成员
- 具有引用数据成员的结构不是文本类型吗?
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员
- 我将如何访问类中结构的数据成员
- 访问嵌套结构的数据成员并将结构传递到功能C
- 如何在STD :: MAP中找到具有至少一个数据成员等于密钥的结构的元素
- 为什么C++编译器不优化对结构数据成员的读取和写入,而不是不同的局部变量?
- 有没有办法将数组行为作为静态结构数据成员?
- 从私有结构数据成员访问类公共成员函数C++
- 单个结构数据成员的奇怪指针地址
- 扩展类/结构C++数据成员的更好方法
- 使用 std::equal_to 等测试特定的结构数据成员
- 需要获取结构数据成员的偏移量
- C++正在将文本文件读取到结构数据成员中