Click to See Complete Forum and Search --> : Reading from a file including null character
Kohinoor24
February 1st, 2002, 05:32 AM
I have some contents in 1 file.Iam reading that into an other file.But it stops reading when it encounters the null character.Is there any other fuction which reads the entire file if the file contains Null also.I want to read the entire file.
I have the following code:
const char cCc ={'\x5A'};
const char Length [] = {'\x00','\x00'};
const char IDM[] = {'\xD3','\xAB','\xCA','\x00','\x00','\x00'} ;
//*A class
class C_ptx
{
public:
C_ptx()
{
m_ptx.open("First",ios::out|ios::binary);
}
void Createstream()
{
m_ptx.write(&cCc,sizeof(cCc));
m_ptx.write(Length,sizeof(Length));
m_ptx.write(IDM,sizeof(IDM));
}
ofstream & returnstream()
{
return m_ptx;
}
private:
ofstream m_ptx;
};
*/class ends here
/*Main Fuction*/
void main()
{
C_ptx T1;
T1.Createstream();
ofstream& s1 =T1.returnstream();
if(!s1.eof())
{
s1.seekp(0,ios::end);
}
int s =s1.tellp();
char *buffer= new char[s];
ifstream r1("First",ios::in|ios::binary);
// Here When Iam reading from the "First" file it stops reading when it encounters the null character.Is there any other fuction which reads the entire file if the file contains Null
r1.getline(buffer,s);
ofstream m_mixeddata("second",ios::out|ios::binary);
m_mixeddata.write(buffer,sizeof(buffer));
}
Help!!
Kohinoor
NMTop40
February 1st, 2002, 07:45 AM
A few things:
1. I think a stream opened for output only is always at eof. I'm not sure you can do seek on it. For that you have to open for write and read.
2. You have not closed your stream before you try to read the same file for input. Although the stream logically knows all the data, it may have buffered some so physically the file either won't open at all or may be incomplete or even empty.
3. You are using text reading functions (getline) for a binary file. You should know, by the way, that the standard streaming functions are all text, although you can write your own for your own classes that output / input binary. All ios::binary does is not convert line endings. (It's annoying because I also thought ios::binary should cause a 4-byte integer to be streamed with 4 bytes).
4. To read from a binary file you may want to use read() member of istream and to write you may want to use write().
5. You might want to use the flashy istreambuf_iterator< >. This would be done as follows (but doesn't work on Windows for some unknown reason)
#include <iterator>
#include <fstream>
#include <vector>
using namespace std; // for illustration convenience
ifstream& ifs=getStream(); //(open stream we have somewhere)
istreambuf_iterator<char> ifStart( ifs );
istreambuf_iterator<char> ifEnd;
vector<char> buffer( ifStart, ifEnd );
if you want to read binary integers, just change <char> to <int> throughout.
This workaround that Scott Meyers gives doesn't work for me on Windows either.
vector<char> buffer;
buffer.reserve( distance( ifStart, ifEnd ) ); // works fine
copy( ifStart, ifEnd, back_inserter(buffer) ); // for me this copies nothing
(Anyone know if Dinkumware has bug-fixes for this?)
The best things come to those who rate
NMTop40
February 1st, 2002, 08:19 AM
ok this worked:
ifs.seekg( 0, ios_base::beg );
after the distance() call, but only with the copy function.
Alternative is not to reserve. That means more reallocations but means it doesn't have to move the file pointer as much.
You can only do this to read the whole file. If you want to read part of the file, use resize() then read() not copy and back_inserter. (Probably better anyway).
The best things come to those who rate
Kohinoor24
February 4th, 2002, 01:57 AM
I have closed the file as u told and instead of the getline function I used the read function.How can I integerate the flashy iterator code into my code.!!!
This is a very important part of code for me as I have to read all data from a file to another if it haves spaces also .
I dont know it is reading only up to characters preceding the space in the file.
Kohinoor
NMTop40
February 4th, 2002, 06:36 AM
My code showed you how to load all the contents of a file into memory as a raw vector of chars.
Files have structure - now what you need to do is interpret the structure of the file into a class structure.
istreambuf_iterator skips over whitespace characters (doesn't interpret them as terminators).
I do have to say though that I find BSTR a much nicer format to work with for file I/O. Doesn't rely on terminators/separators
If you are working on UNIX you can write your own BSTR-based class to work with for file I/O. (You can do so if working on Windows too and don't want to use ATL, or want to work with 1-byte characters (BSTR is based on Unicode)
A BSTR (in COM) is a 4-byte length (binary) followed by the string. The length is number of characters that follows, not number of bytes (which is twice as many), thus the length in bytes is actually 2n+4.
Of course, this would make your files not readable or editable in a text-editor. Depends on whether you consider this a good thing or a bad thing.
The best things come to those who rate
NMTop40
February 4th, 2002, 08:09 AM
actually, a BSTR does contain a terminating null character as well, but there can also be embedded nulls. The terminating null is not counted in its length but is always there.
However the provided functions of CComBSTR (WriteToStream and ReadFromStream) do not persist these null terminators.
You can cast to LPCWSTR but obviously this will go only to the first null, but you can also create wstring from one easily enough as that takes a pointer and a length as one of its constructors.
CComBSTR already wraps the class, and although it is not documented it has an operator LPCWSTR which returns the address of the first character if not NULL, and NULL otherwise.
The best things come to those who rate
manojvibhute
September 3rd, 2002, 08:15 AM
I think End result wanted is same copy of the file including white spaces and everything. So better use copyfile function to make its copy
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.