| | |
Writing To a File in Binary Mode
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
>>I have used char *per because 'write' function only accepts pointer to character.
False. You can put anything you want to there -- just typecast it to char*
But you still failed to correct the problem. per is still an unallocated pointer which will crash your program.
C++ Syntax (Toggle Plain Text)
#include<fstream.h> #include<iostream.h> #include<string.h> using namespace std; class emp { public: char *per; void write() { ofstream out("ss.txt",ios::app | ios::binary); cout<<"Enter some character ....\n"; per = new char[100]; cin>>per; //out.write(per,sizeof(per)); out.write(per,strlen(per)); } }; void main() { emp e; e.write(); }
EDIT: Didn't see the new[] Ouch. My bad, 100%.
Job well done on the program. However, when write is now called multiple times, each time it will allocate 100 bytes of memory to the pointer. This is called a memory leak. To fix that, make sure you only allocate the memory once (call new once).
Job well done on the program. However, when write is now called multiple times, each time it will allocate 100 bytes of memory to the pointer. This is called a memory leak. To fix that, make sure you only allocate the memory once (call new once).
Last edited by Clockowl; Aug 13th, 2008 at 11:44 am.
![]() |
Similar Threads
- Writing PCM as raw data in C/C++ in Windows (C++)
- C++ How to write strings to a Binary file? (C++)
- Problems writing stdout to a file, please help! (Perl)
- Saving to a file from an array in C ? (C)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
- file types: eliminating leading white spaces (C)
- Windows API functions to read and write files in C (C)
Other Threads in the C++ Forum
- Previous Thread: How to count the number of charachters a user has inputted?
- Next Thread: Need help on a sorting algorithm function.
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





