reading bytes of a file and producing the file back.

Hi. I've been trying to read a file byte by byte into a .txt file and then reproduce the original file back from the .txt file. For this
I want to include ALL characters(even white spaces).
Also I don't want that extra character hanging on at the end of the file.
I've tried this.

#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) //or !fin.eof() or fin.get(s) or fin>>x
{
fin.get(s);
fout<<s; //or fout.put(s)
fout<<".";
}

return 0;}


But this doesn't work as expexted.
Can somebody post the complete code for this project.
Last edited on
sanyam wrote:
Can somebody post the complete code for this project.

That's not really how we work.

Check this out and look at binary files:
http://cplusplus.com/doc/tutorial/files/


To read it:
1
2
3
4
ifstream fin ("file.txt", ios::binary); // Opens a file in binary mode
ifstream::pos_type size = fin.tellg(); // This is the size (in bytes of the file)
char* memblock = new char [size]; // Create a memory block that will hold the data
fin.read(memblock, size); // Read from the file to the memory block. 


Then to write it again:
1
2
ofstream fout("bytesequence.txt", ios::binary); // Opens a file
fout.write(memblock, size); // Writes the data. 

no actually i got my error
when i wrote fin it got an extra char
Topic archived. No new replies allowed.