Привет, я пытаюсь читать в двоичном файле, который я создал из другого набора кодов. Например. Использование a.cpp создать двоичный файл и с помощью B.cpp прочитать файлЧтение двоичного файла, созданного другим набором кодов C++
Например: в a.cpp я создал файл, используя со следующей структурой ниже
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;
};
В А. 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. Например: я ввожу 3 набора данных в A.cpp, когда я прочитал из B.cpp i, я получаю только размер 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 ??
Im только новичок в C++, если я ошибаюсь ни на что pls, дайте мне знать. спасибо заранее
MVCE
a.cpp
#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();
}
B.cpp
#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();
}
Это поможет использовать [MVCE] (http://stackoverflow.com/help/mcve) –
@EdHeal hi i edit my post pls дайте мне знать, если вам нужно что-нибудь еще, спасибо alot –
Пожалуйста, сузите проблема намного больше –