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?
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.
#include <iostream>
#include <cstdio>
#include <fstream>
usingnamespace std;
string ff;
bool read(constchar 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(constchar *addres)
{
ifstream file(addres);
if (!file) return 1;
file.seekg(0, ios::end);
unsignedlonglong flength = file.tellg();
file.seekg(0, ios::beg);
ff += ".log";
ofstream log(ff.c_str());
if (!file) return 1;
char c;
string s;
unsignedlong 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;
}
elseif (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;
}
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)
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.
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 = newchar[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.
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