| | |
how to read a binary file data to a monitor
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 15
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: 360
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
Views: 1410 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






