Binary file (EOF)

I'm reading a binary file and I want to read all of the letters, but I have noticed that it dosen't read all of the letters and I think it is because eof apears as data. Can someone help me get around this?
Binary files don't have letters, they have bytes. Everything in a binary file appears as data, because it's all data.

You're probably reading from the file incorrectly. But I can't give you any guidance without seeing what you're doing.
1
2
3
4
5
6
7
8
9
10
11
12
13
char[] read(char[] filename){
    ifstream ifile;
    ifile.open(filename, ios::binary);
    if(ifile.is_open){
        ifile.seekg(0, ios::end); //Get length of file
        int length = ifile.tellg(); //Store length of file
        ifile.seekg(0, ios::beg); //Reset Position of pointer to 0
        char* text = new char[length]; //Set length of char* to file length
        ifile.read(text, length); //Read File upto Length limit
        ifile.close();
        return text;
    }
}


There may be a few errors with this but I'm sure if there are you'll be able to iron them out easy enough
Last edited on
eehhh..

returning an allocated char array is not a great idea. It passes ownership (calling function is responsible for delete[]ing), and char arrays are generally interpretted as null terminated text... which this data is not.

So while nothing there is technically "wrong", it's very error prone and I would advise against it.
Null terminating character is not the problem.
But the problem is that the whole data is copyed to memory. But I quess I could just get the lenght of the file and read that many characters?

By the way I'm trying to read text from programs. I'm testing it on itself :)
But I'm having another problem, it dosen't read newline characters. When I'm reading a text file, like my .cpp file, it ignores spaces. I mean the program is sapoust to read all letters and several other characters, like dots and quotes, and if nothing matches to write it to log file and go to a new line.
I see that new line characters are also not writen to log file.

And, apperently, my file is still not fuly read.

Here is my code.
Compile it, run it, drag the exe file ower the console and it will open the file. See if the log file will contain my message.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <cstdio>
#include <fstream>

using namespace std;

string ff;

bool read(const char addres[]);

int main(int argc, char *argv[])
{
    if (argc!=1)
    {
        ff = (char*)argv;
        read((char*)argv);
    }
    else {
        do {
            cout << "You have to give an addres of the file.\n\
You can give addres by opening a file with this program or by writeing the addres here:\n";
            getline(cin, ff);
            if(ff[0] == '\"')
            {
                ff.erase(ff.begin() + 0);
                ff.erase(ff.begin() + ff.length() - 1);
            }
        } while (read(ff.c_str()));
    }
    
    return 0;
}

bool read(const char *addres)
{
    ifstream file(addres);
    
    if (!file) return 1;
    
    file.seekg(0, ios::end);
    unsigned long long flength = file.tellg();
    file.seekg(0, ios::beg);
    
    ff += ".log";
    
    ofstream log(ff.c_str());
    
    if (!file) return 1;
    
    char c;
    string s;
    unsigned long r = 0;
    bool k = 0;
    
    while(flength--)
    {
        file >> c;
        if (c=='\n'){ cout<<1; cin.get();}
        if (c==0 || c=='\n') {
            if (r>=2) {
                log << s << '\n';
                r = 0;
                s.clear();
            }
            continue;
        }
        if ((c>='A' && c<='Z') || (c>='a' && c<='z'))
        {
            r++;
            s += c;
        }
        else if (r >= 1)
        {
            switch (c) {
                case' ': // I could have compared this
                case'_': // like I did with letters
                case'.': // but I'm still not sure what characters I want
                case',':
                case'\'':
                case'\"':
                case'-':
                case'!':
                case'?':
                case'(':
                case')':
                case'+':
                case'@':
                case'#':
                case'$':
                case'%':
                case'^':
                case'&':
                case'*':
                case'{':
                case'}':
                case'[':
                case']':
                case'<':
                case'>':
                case'|':
                case'/':
                case'\\':
                case':':
                case';':
                case'~':
                    s += c; break;
                default: k = 1;
            }
        }
        if (k)
        {
            if (r>=2) log << s << '\n';
            if (r) {
                r = 0;
                s.clear();
            }
            k = 0;
        }
    }
    
    file.close();
    log.close();
    return 0;
    
}
Last edited on
ifstream file(addres);
That opens the file in text mode, use
ifstream file(addres, std::ios::binary);
if you intend to measure its length with tellg() (there is no need for that, by the way)

file >> c;
This skips whitespace (spaces, endlines, and tabs). Use
file.get(c)
if you intend to process every byte from a file open in binary mode, or
every character from a file open in text mode (although file >> c would work in the latter case, if you don't forget file >> noskipws first)
Last edited on
Sorry about my code, I've took it strait from something I used quite a while ago, on my project it's using global variables so it's all safe... Just take a little fidget about with some stuff and what I posted will work fine.
Why is there no need for finding file lenght?

And did you tested the program? It still dosen't read the message in itself...
Again it may have a couple errors but I think this should be a safety rectified version that'll work straight off.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string read(string filename){
    ifstream ifile;
    ifile.open(filename.c_str(), ios::binary);
    if(ifile.is_open){
        ifile.seekg(0, ios::end); //Get length of file
        int length = ifile.tellg(); //Store length of file
        ifile.seekg(0, ios::beg); //Reset Position of pointer to 0
        char* temp = new char[length]; //Set length of char* to file length
        ifile.read(text, length); //Read File upto Length limit
        ifile.close(); //Close file
        string text = temp; //Assign string to char[]
        delete temp; //delete memory assigned on heap
        return text; //safely return string
    }
}


Or you can use the previously posted code (with a few tweeks to the position to the operator[] cos I don't think it's in the right place) but remember to delete the memory assigned to the heap after you've finished with it...
If you don't know about heap memory then just use the code here.
I have tryed your way and it works perfectly. Thanks

But I still don't understand why my way dosen't work?
Compare it to mine, see if you have everything in yours what this has.

Open file
Get length
Some where to store output with size of length
Read file for length size
Store file
Close file
Cleanup resources if heap memory used
return
Topic archived. No new replies allowed.