我正在尝试进行单一分配(第一年),其中任务是读取一个充满记录的文件(在这种情况下是伪学生记录),将它们插入代码中的结构数组中,写下该结构将数组转换为.dat文件,然后重新读入该文件并进行验证。
我一直收到关于将我的结构转换为char *(第155行)的错误
请不要太熟悉答案,因为我只编写了几个月ahah
/*
* File: lab3.cpp
* Author: tom
*
* Created on August 19, 2015, 12:02 AM
*/
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
//Structure declaration area
struct marks // A structure for user data
{
int studentnumber;
float ass1;
float ass2;
float ass3;
float ass4;
int labs;
float exam;
float total;
};
//Function Prototyping
void structinsert(ifstream&);
void binarywrite(ofstream&);
int binaryread(ifstream& , marks);
//Global Array for student marks
marks students [100];
int main(int argc, char* argv[])
{
ifstream fin;
ofstream fout;
int recordsread = 0;
fin.open(argv[1]); //Input file opened from cmdline args
fout.open(argv[2]); // Output file opened from cmdline args
if (fin.fail())
{
cerr << "Input file not found. Terminating..." << endl;
}
else if (fout.fail())
{
cerr << "Output file not found. Terminating..." << endl;
}
else
{
structinsert(fin);//Function which places data into the global "student" array
binarywrite(fout);
fin.close();
fout.close();
fin.open(argv[2]);
int cnt = 0;
while((!fin.eof()) && (cnt < 100))
{
fin.read(static_cast<char *>(&students[cnt]), sizeof(marks));
cnt++;
}
cout << "Records Read: " << recordsread << endl;
}
return 0;
}
void structinsert(ifstream& input)
{
float temp;
float value;
// Ignore first line of headings
input.ignore(150, '\n');
for (int x = 0; x < 100; x++)
{
input >> students[x].studentnumber;
input.ignore (1);
temp = input.peek();
if (temp == '\t')
{
input.ignore(1,'\t');
students[x].ass1 = 0;
}
else
{
input >> students[x].ass1;
input.ignore(1,'\t');
}
temp = input.peek();
if (temp == '\t')
{
input.ignore(1,'\t');
students[x].ass2 = 0;
}
else
{
input >> students[x].ass2;
input.ignore(1,'\t');
}
temp = input.peek();
if (temp == '\t')
{
input.ignore(1,'\t');
students[x].ass3 = 0;
}
else
{
input >> students[x].ass3;
input.ignore(1,'\t');
}
temp = input.peek();
if (temp == '\t')
{
input.ignore(1,'\t');
students[x].ass4= 0;
}
else
{
input >> students[x].ass4;
input.ignore(1,'\t');
}
input >> students[x].labs;
input.ignore(1,'\t');
temp = input.peek();
if (temp == '\n')
{
input.ignore(1,'\t');
students[x].exam = 0;
}
else
{
input >> students[x].exam;
input.ignore(1,'\t');
}
}
}
void binarywrite(ofstream& output)
{
output.write(reinterpret_cast<char*>(&students),100*sizeof(marks));
}