how to read a binary file data to a monitor
Please support our C++ advertiser: Programming Forums
![]() |
•
•
Posts: 13
Reputation:
Solved Threads: 0
Please compare the two codes below why is the first one displaying output to monitor where as the second code is displaying some unwanted characters.
C++ Syntax (Toggle Plain Text)
CODE 1 ..... #include <fstream.h> #include <string.h> void main() { fstream File("test_file.txt",ios::out | ios::in | ios::binary ); char arr[13]; strcpy(arr,"Hello World!"); //put Hello World! into the array File.write(arr,5); //put the first 5 symbols into the file- "Hello" static char read_array[10]; //I will put the read data, here File.seekg(ios::beg); File.read(read_array,5); //read the first 3 symbols- "Hel" cout << read_array << endl; //display them File.close(); } CODE 2.... #include <fstream.h> #include <string.h> void main() { fstream File("test_file.txt",ios::out | ios::in | ios::binary ); char * memblock; ifstream::pos_type size; //int size; File.seekg(ios::ate); size = File.tellg(); memblock = new char[size]; File.read(memblock,size); //cout<<*memblock; //cout<<"\n"; cout<<memblock; File.close(); }
•
•
Posts: 285
Reputation:
Solved Threads: 53
CODE 1 may end in problems, too, because of the missing terminating 0 in read_array
CODE 2 is missing a rewind to begin of file before File.read(memblock,size); and has the same problem with the terminating 0. Replace memblock = new char[size]; by memblock = new char[size + 1]; memblock[size] = 0;
CODE 2 is missing a rewind to begin of file before File.read(memblock,size); and has the same problem with the terminating 0. Replace memblock = new char[size]; by memblock = new char[size + 1]; memblock[size] = 0;
Last edited by jencas : Aug 22nd, 2008 at 10:27 am.
If you are forced to reinvent the wheel at least try to invent a better one!
Please help us helping you and read carefully: http://www.daniweb.com/forums/thread78223.html
Please help us helping you and read carefully: http://www.daniweb.com/forums/thread78223.html
You would also need to delete the memory you've freed, or else you'll get a memory leak
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
Both suffer from having void main as well.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
![]() |
Similar Threads
Other Threads in the C++ Forum
- Send data on a serial port (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- Antivirus Gold/SpySheriff/Smitfraud Problems (Viruses, Spyware and other Nasties)
- Pop-ups and redirection (Viruses, Spyware and other Nasties)
- Hijack THis Log READ ASAP! (Viruses, Spyware and other Nasties)
- more aurora probs (Viruses, Spyware and other Nasties)
- Huge Problem Please Help! (Viruses, Spyware and other Nasties)
- HijackThis logfile of system (Viruses, Spyware and other Nasties)
- Rundll32.exe Problems = [ Need some assistance (Viruses, Spyware and other Nasties)
- registry help please (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: MouseMove in Visual C++
- Next Thread: Help with creating a class
•
•
•
•
Views: 1075 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode