| | |
how to read a binary file data to a monitor
![]() |
•
•
Join Date: Aug 2008
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(); }
•
•
Join Date: Dec 2007
Posts: 359
Reputation:
Solved Threads: 69
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 11:27 am.
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
![]() |
Similar Threads
- Send data on a serial port (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- 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
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes cloud code coding compaitibility compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error factorial file floatingpoint forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer iso java lib linkedlist linker loop looping loops map math matrix memory multiple net news node output parameter payment pointer practice problem program programming project projectassignmenthelp protection python random rank read recursion reference rpg skills string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






