| | |
how to read a binary file data to a monitor
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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 beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






