2017-01-15 10 views
0

안녕하세요, 저는 다른 코드 집합에서 만든 이진 파일을 읽으려고합니다. 예 : 바이너리 파일을 작성 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(); 

} 
+0

[MVCE] (http://stackoverflow.com/help/mcve) –

+0

@EdHeal 안녕하세요. 내 게시물 pls 편집 다른 도움이 필요하면 알려 주시기 바랍니다 –

+0

문제가 훨씬 더 –

답변

1

(A.cpp에서 사용) subjects 구조체의 마지막 멤버 grade이며 약되어야 1-8 바이트.
구조체 studentsubjects (B.cpp에서 사용)의 마지막 멤버는 char[80]이며 80 바이트 여야합니다. B.cpp의 구조가 A.cpp의 것보다 클 것을 의미합니다.

결과적으로 같은 크기의 바이트가 주어지면 그 크기가 세 구조체 subjects의 크기 일 경우 구조체 studentsubjects의 경우 세 개가 충분하지 않습니다.

이러한 유형의 실수를 피하려면 구조체 선언을 작성하여 헤더 파일을 읽고 쓰고 구조체를 읽고 쓰는 소스 코드의 헤더 파일을 사용해야합니다.

+0

안녕하세요 @ @MikeCAT 실제로이 'A.ccp'와 'B.ccp'는 '아'와 'B.h'가 같은 프로젝트에 있으며, 동일한 열거 형 '등급'을 선언하고 ' Bh 'from'Ah '그러나 중복 오류가 많이 나타납니다. 그래서 나는 구조를 다시 선언했다. 두 헤더 파일에서 같은 열거 형을 사용하기 위해 할 수있는 일이 있습니까? –