C++ 读取由另一组代码c++;
嗨,我想读入一个二进制文件,这个文件是我用另一组代码创建的。 例如,使用A.cpp创建二进制文件,使用B.cpp读取文件 例如: 在A.cpp中,我使用以下结构创建了一个文件C++ 读取由另一组代码c++;,c++,binaryfiles,C++,Binaryfiles,嗨,我想读入一个二进制文件,这个文件是我用另一组代码创建的。 例如,使用A.cpp创建二进制文件,使用B.cpp读取文件 例如: 在A.cpp中,我使用以下结构创建了一个文件 struct assignment { char atitle [MAX]; int weight; int markupon; double marks; }; struct subjects { char code [MAX]; char subtitle [M
struct assignment
{
char atitle [MAX];
int weight;
int markupon;
double marks;
};
struct subjects
{
char code [MAX];
char subtitle [MAX];
int NoOfTask;
assignment AStructure [MAX];
int finalmarks;
grades grade;
};
在A.cpp中,我使用以下代码来获取文件中的记录数,它工作正常
int getNoOfRecords (fstream& afile, char filename [])
{
afile.open (filename, ios::in | ios::binary);
if (!afile)
{
afile.close ();
return 0;
}
// move read marker point to end of
// the last record
afile.seekg (0, ios::end);
int totalBytes = afile.tellg ();
int noOfsubjects = totalBytes / sizeof (subjects);
afile.close ();
return noOfsubjects;
}
在B.cpp中,我有以下结构
struct studentassignment
{
char atitle [MAXN];
int weight;
int markupon;
double marks;
};
struct studentsubjects
{
char code [MAXN];
char subtitle [MAXN];
int NoOfTask;
studentassignment AStructure [MAXN];
int finalmarks;
char grade [MAXN];
};*
在B.cpp中,我使用下面的代码来获得记录数,但是我得到的结果与在A.cpp中得到的结果不同。例如:我在A.cpp中输入了3组数据,当我从B.cpp读取数据时,我只得到了2的大小。从文件中读取的数据也不正确
int NoOfRecords (fstream& afile, char filename [])
{
afile.open (filename, ios::in | ios::binary);
if (!afile)
{
afile.close ();
return 0;
}
afile.seekg (0, ios::end);
int totalBytes = afile.tellg ();
int noOfsubjects = totalBytes / sizeof (studentsubjects);
afile.close ();
return noOfsubjects;
}
是否有我做错了什么,或者是没有办法使用不同的cpp文件读取文件
我只是初学C++的人,如果我有什么不对的地方,请告诉我。提前谢谢
MVCE
A.cpp
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数int MAX=80;
枚举等级{HDist、Dist、Credit、Pass、Fail};
结构赋值
{
字符数[MAX];
整数权重;
int-markon;
双标记;
};
结构主题
{
字符码[MAX];
字符字幕[MAX];
国际努夫茨克;
赋值结构[MAX];
国际金融市场;
年级;
};
int main()
{
afile.open(“filename.dat”,ios::out | ios::binary);
afile.seekg(0,ios::end);
受试者的性别;
cout>s代码;
库特·努夫茨克;
coutstructsubjects
(用于A.cpp
)中的最后一个成员是grade
,应该是大约1-8字节。
structstudentsubjects
(用于B.cpp
)中的最后一个成员是char[80]
,应该是80字节。
这意味着B.cpp
中的结构将大于A.cpp
中的结构
因此,如果给定的字节数与三个struct主题的大小相同,那么对于三个structstudentsubjects
来说就不够了
为了避免这种类型的错误,您应该在头文件中写入要读取和写入的结构声明,并使用读取和/或写入结构的源代码中的头文件。这将有助于提供@EdHeal hi i edit my post请告诉我您是否还需要其他任何东西,谢谢。请缩小问题的范围oreHi@MikeCAT实际上这个'A.ccp'和'B.ccp'与'A.h'和'B.h'在同一个项目中,它试图在'B.h'中声明与'A.h'相同的枚举'grade'和结构,但出现了大量重复错误。因此我重新声明了结构。我可以在两个头文件中使用相同的枚举吗?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int MAX = 80;
enum grades {HDist, Dist, Credit, Pass, Fail};
struct assignment
{
char atitle [MAX];
int weight;
int markupon;
double marks;
};
struct subjects
{
char code [MAX];
char subtitle [MAX];
int NoOfTask;
assignment AStructure [MAX];
int finalmarks;
grades grade;
};
int main ()
{
afile.open ("filename.dat", ios::out | ios::binary);
afile.seekg (0, ios::end);
subjects s;
cout << "Subject Code: ";
cin >> s.code;
cout << "Subject Name: ";
cin.ignore();
cin.getline(s.subtitle, MAX);
cout << "No of assessment task: ";
cin >> s.NoOfTask;
cout << endl;
for (int i = 0; i < s.NoOfTask; i++)
{
cout << "Task " << i+1 << " information" <<endl;
cout << "Title: ";
cin.ignore();
cin.getline(s.AStructure[i].atitle, MAX);
cout << "Weight: ";
cin >> s.AStructure[i].weight;
cout << "Upon: ";
cin >> s.AStructure[i].markupon;
cout << endl;
s.AStructure[i].marks = 0;
total = total + s.AStructure[i].weight;
}
afile.write (reinterpret_cast <const char *>(&s), sizeof (subjects));
afile.close ();
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int MAXN= 80;
const int MAXN0= 10;
struct studentassignment
{
char atitle [MAXN];
int weight;
int markupon;
double marks;
};
struct studentsubjects
{
char code [MAXN];
char subtitle [MAXN];
int NoOfTask;
studentassignment AStructure [MAXN];
int finalmarks;
char grade [MAXN];
};
int NoOfRecords (fstream& , char []);
void getrecord(fstream& afile, char filename [] , int, studentsubjects);
int main()
{
fstream afile;
subjects b;
int size;
size = NoOfRecords (afile, "filename.dat");
cout << size << endl;
for (int i = 0; i < size; i++)
{
getrecord(afile, "filename.dat", i+1, b);
cout << b.code << endl;
}
}
int NoOfRecords (fstream& afile, char filename [])
{
afile.open (filename, ios::in | ios::binary);
if (!afile)
{
afile.close ();
return 0;
}
afile.seekg (0, ios::end);
int totalBytes = afile.tellg ();
int noOfsubjects = totalBytes / sizeof (studentsubjects);
afile.close ();
return noOfsubjects;
}
void getrecord(fstream& afile, char filename [], int i, studentsubjectsb)
{
afile.open (filename, ios::in | ios::binary);
afile.seekg ((i) * sizeof (studentsubjects), ios::beg);
afile.read (reinterpret_cast < char *>(&b), sizeof (studentsubjects));
afile.close ();
}