Binary Files

So the thing is...
I copyed content from a text file to a binary file.
Text file has 86 lines, one word in each line.
After copying the content from text file to binary file, i did check with hex editor if everything is OK inside that binary file, and everything is OK there.
But the problem is, if i try to count the lines inside that binary file, it counts till line 71 and stops. Also when i try to print all the words, it prints all of them till line 71. And i've no idea why. Heres my code.
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
#include <iostream>
#include <fstream>
using namespace std;

void Binary_Search(const string& filename)
{
    std::fstream file;
    int lines = 1;
    char c;
    file.open( filename.c_str());
    if (file.is_open())
    {
        cout << "The file is opened"<< endl;
    }
    else
    {
        cout << "Error opening file"<< endl;
    }
    file.get(c);
    while(file)
    {
        if(c == '\n')
        {
            cout << endl;
            lines++;
        }
        else cout << c;
        file.get(c);
    }
    cout << "\n"<<lines;
};



int main()
{
    const unsigned int BUFFER_SIZE = 3;
    char buffer[BUFFER_SIZE];
    fstream fin ("rezervetie.txt", ios::in);
    fstream fout ("Binary.bin", ios::out);
    while (fin)
    {
        fin.read (buffer, BUFFER_SIZE);
        fout.write (buffer, fin.gcount());
    };
    Binary_Search("Binary.bin");
    fin.close ();
    fout.close ();
}

Close the output file before you try to read from it.
Also, i think you are reading from the file differently from the way you packed the data.

If you pack data in the binary file in 3 bytes(line 38), then you have to read that way.
Thanks alot, Works.
Topic archived. No new replies allowed.