안녕하세요, 저는 다른 코드 집합에서 만든 이진 파일을 읽으려고합니다. 예 : 바이너리 파일을 작성 A.cpp를 사용하여, 예를 들면 파일다른 코드 집합으로 작성된 이진 파일 읽기 C++
를 읽어 B.cpp 사용 : A.cpp에 제가
A.에서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 나는 파일에있는 레코드의 수를 얻기 위해 다음 코드를 사용하고 잘 동작합니다.
B.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;
}
내가 가지고있는 다음과 같은 구조
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 파일을 사용하여 파일을 읽을 수있는 단지 방법이없는 뭔가가 있나요?
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 안녕하세요. 내 게시물 pls 편집 다른 도움이 필요하면 알려 주시기 바랍니다 –
문제가 훨씬 더 –