2017-01-15 7 views
0

こんにちは、別のコードセットから作成したバイナリファイルを読み込もうとしています。 例バイナリファイルを作成するために、A.cppを使用し、例えばファイル別のコードセットで作成されたバイナリファイルを読み込むC++

を読むためにB.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; 
}; 

以下、下記の構造を使用してファイルを作成したA.cppで 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++で何かが間違っていると私に教えてください。事前のおかげ

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

問題が多いmore –

答えて

1

A.cppで使用される)構造体subjectsの最後のメンバーはgradeであり、それは周りでなければなりません1〜8バイト。
struct studentsubjectsの最後のメンバー(B.cppで使用)はchar[80]であり、80バイトである必要があります。 B.cppの構造がA.cppの1より大きくなることを意味します。

結果として、同じ数のバイトが与えられ、それが3つの構造体subjectsのサイズにすぎない場合、3つの構造体studentsubjectsでは十分ではありません。

このタイプの間違いを避けるには、構造体の宣言をヘッダファイルに読み書きし、構造体を読み書きするソースコードからヘッダーファイルを使用する必要があります。

+0

こんにちは@MikeCATこの 'A.ccp'と 'B.ccp'は 'Ah'と 'B.h'と同じプロジェクトにあり、同じ列挙型の '等級'を宣言し、 Bh 'を' Ah 'から削除しましたが、重複エラーが多く発生しました。したがって私は構造を再宣言しました。私は両方のヘッダーファイルで同じ列挙型を使用するために何ができるのですか? –

関連する問題