reading bytes of a file

I compiled the following code

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

int main()
{
ifstream fin;
fin.open("file.txt",ios::binary);
ofstream fout;
fout.open("bytesequence.txt",ios::binary);
int a;
char s;

while(!fin.eof())
{
fin>>s;
fout<<s+0;
fout<<".";
}

return 0;}


the file.txt contains
abc
def
hij

the bytesequence.txt changes to
97.98.99.100.101.102.104.105.106.106.

first I'm not getting the <enter> character then i',m getting two 106's. Why's that?
When you read using operator>> it will skip all white space characters. If you don't want to ignore any characters, use fin.get(s); instead.

You get 106 twice at the end because of this problem: http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
i changed it to this...

while(fin.get(s))
{
fin.get(s);
fout.put(s+0);
fout<<".";
}

the output is

b.
.d.f.
.i.j.

some characters are missing and there not integers
Last edited on
Some characters are missing because you call get twice. Remove the second call.

The put function outputs characters. If you want integers you can output as you did before fout << s + 0; or use a cast fout << (int) s; or fout << static_cast<int>(s);
thanx.. peter87
I've another related problem at http://www.cplusplus.com/forum/general/70353/
please help!
I posted duplicate coz i needed it urgently.
Topic archived. No new replies allowed.