searching binary file

Hello I am completely baffled by bin file read/write and positioning. Here I wish to search a chemical name info by symbol. Here is my struct

1
2
3
4
5
6
7
struct chemicalElement {

    int atomicNumber;
	char name[118];
	char symbol[3];
	float mass;
};


I successfully wrote to file.dat as binary but having problem in reading and searching...here is my func...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void searchSymbol(char sym, chemicalElement chemEl[]){

    ifstream binFile;
    unsigned short int i=0;

    binFile.open("chemicalEl.dat", ios::binary);

    if(binFile.good())
        cout<<"File read for search"<<endl;

    while(binFile.good()){/////Here I am lost what to do and how
        binFile.read((char*)&chemEl[i], sizeof(chemicalElement));
        if(strcmp(chemEl[i].symbol, sym)==0 )
            cout<<chemEl[i];///display all info about that element
        else
            i++;

    }
    binFile.close();


}
Last edited on
I would recommend not opening the file as binary if you want to search for text within it.
closed account (LbXjz8AR)
Try using the topic of classes and objects. I hope u get my point.
I managed to search the file but my output is like this


262 Dunium Db   105
272 Lawrencium  Lr     103
.......


using tab for every space

my binary file write function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

struct chemicalElement {

    int atomicNumber;
	char name[17];
	char symbol[3];
	float mass;

};


void makeBin(chemicalElement chemEl){

    ifstream read;
    ofstream rite;

    read.open("chemicalElements.txt", ios::in);//open for read
    rite.open("chemicalEl.dat", ios::binary);//open binary file for write
    if (read.good())
        cout<<"Open success for read"<<endl;

    while(read.good()){
        read>>chemEl.mass;
        read>>chemEl.name;
        read>>chemEl.symbol;
        read>>chemEl.atomicNumber;
        rite.write((char*)&chemEl, sizeof(chemicalElement));//write to binary file

    }
    read.close();

    rite.close();
}

//Search binary file by mass 

void getInfo(float Mass, chemicalElement chemEl){

 ifstream binFile;


    binFile.open("chemicalEl.dat", ios::binary);

    if(binFile.good())
        cout<<"File read for search"<<endl<<endl;


    while(binFile.good()){
        binFile.read((char*)&chemEl, sizeof(chemicalElement));

        if(chemEl.mass>Mass){//search from minimum mass
            cout<<chemEl.mass<<"\t"<<chemEl.name<<"\t"<<chemEl.symbol<<"\t"<<chemEl.atomicNumber<<endl;
           }
        else{
            if(binFile.eof())
                cout<<"Element not found";
            }
    }
    binFile.close();

}


I want equal spacing for all outputs
Last edited on
closed account (LbXjz8AR)
try to put "\n" in alterante in line 52. after chemE1.name<<\n<<..
try putting \n as i showed. It will give you the mass, namee, symbol and atomic number. See if it works.
1
2
3
void searchSymbol(char sym, chemicalElement chemEl[]){
...
if(strcmp(chemEl[i].symbol, sym)==0 )

sym should be declared as char*, not char.

Also why are you passing and populating the whole array? Should you just return the one item that you find?
void searchSymbol(char &sym, chemicalElement& chemEl){
Similarly, getInfo() needs to pass chemEl by reference if you want the caller to have access to the result:
void getInfo(float Mass, chemicalElement& chemEl){

1
2
3
4
5
6
    while(read.good()){
        read>>chemEl.mass;
        read>>chemEl.name;
        read>>chemEl.symbol;
        read>>chemEl.atomicNumber;
        rite.write((char*)&chemEl, sizeof(chemicalElement));//write to binary file 

This will probably fail at the end of the file. It should be:
1
2
3
4
5
6
7
8
    while(true){
        read>>chemEl.mass;
        read>>chemEl.name;
        read>>chemEl.symbol;
        read>>chemEl.atomicNumber;
        if (!read.good()) break;
        rite.write((char*)&chemEl, sizeof(chemicalElement));//write to binary file
     }


Similarly your read loop should be:
while(binFile.read((char*)&chemEl, sizeof(chemicalElement))) {


Topic archived. No new replies allowed.