|
hi,
Can anybody help me as I need a sample code for taking in data in C++ and then writting it into textfile
Regards,
|
|
|
|
|
you can take data in a variable by cin
and print that data to output stream by cout.
or
u can use file opration for it.
Create file,write data to file and finaly read data from file.
|
|
|
|
|
|
Note that the samples are in C# and VB, but it's trivial to convert that to C++/CLI.
Regards,
Nish
|
|
|
|
|
My understanding of Hashtables is this: If I declare a Hashtable like so: Hashtable *hashMyHashtable = new Hashtable; I don't need to have a corresponding DELETE after having used the NEW for the Hashtable. The reason being that the Hashtable is a Managed object and so I don't need to Delete it in my destructor.
My question is this: If I store an object that was declared with a NEW, and store it in the hashtable, do I need to go back through the hashtable and delete those objects, or does the managed code do that as well?
Example:
MyObject *tempObject = new MyObject;
Hashtable *hashObjects = new Hashtable;
hashObjects->Add(key, tempObject);
So if I store several tempObjects in my hashtable, do I need to traverse through the hashtable and DELETE the tempObjects, or does the garbage collector handle this as well as the hashtable itself?
If I do need to delete them, will a call to hashObjects->clear(); delete the objects?
Thanks,
Jody Blau
|
|
|
|
|
The correct way to work with managed objects in C++/CLI is to use managed references, and the gcnew operator.
MyObject^ tempObject = gcnew MyObject();
Hashtable^ hashObjects = gcnew Hashtable();
hashObjects->Add(key, tempObject); Once all references to a managed object are set to null or go out of scope, the object will be garbage collected at whatever point the CLR decides to do so.
|
|
|
|
|
Justin
I believe he's using the now obsolete MC++ syntax.
|
|
|
|
|
jblau wrote: do I need to go back through the hashtable and delete those objects
No, and you can see this by counting the references to the objects.
MyObject* tempObject = new MyObject; The ref count on the object is now 1.
hashObjects->Add(key, tempObject); The hash table now holds another reference on the object, so the ref count is 2.
Once the tempObject object falls out of scope, the ref count drops to 1. When you empty the hash table, it releases the references on the objects that were in the table, so the ref count drops to 0.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
Laugh it up, fuzzball.
|
|
|
|
|
To add to what Mike says, the objects that you add to the hash table are also managed objects, and thus garbage collected.
If you add unmanaged pointers to the hashtable, you'd have to manually delete the native objects that are pointed to by those pointers.
|
|
|
|
|
I want to use graphic header in visual c++
but it's not possible.
please help me.
great tanks to you.
Bye.
MK4
-- modified at 16:04 Thursday 5th January, 2006
|
|
|
|
|
In what way is it not possible? Do you mean it's not available? If so, there's nothing you can do about it. On the other hand, if you copied it from a Borland C++ installation, and it's not compiling, then you might have to change the Borland specific macros/code to use Microsoft specific or generic syntax.
|
|
|
|
|
Hi
tanks for ur answer
i want to compile some programs used the graphics.h with visual C++ 6.
and i don't know about how the graphics.h works to change it.
please help me.
bye.
MK4
-- modified at 1:49 Saturday 7th January, 2006
|
|
|
|
|
if you have the TC2 graphic.h headers and libs you should able to compile it with VC1.5 ...
My blogs:
http://blog.joycode.com/jiangsheng
http://blog.csdn.net/jiangsheng
http://bloglines.com/public/jiangsheng
Command what is yours
Conquer what is not
---Kane
|
|
|
|
|
|
This question should be posted in the Visual C++[^] forum. This is the C++/CLI forum that's dedicated to the new managed and mixed-mode compiler available in VC++ 2005.
|
|
|
|
|
Will Do.
Thanks Much!
T.J. Barbour
|
|
|
|
|
Hi guys
I've seen some articles talking about CFileDialog with preview and the CFileDialog resource is bigger than the usual CFileDialog. I read about "template dialog" but I don't know how this stuff is implemented.
What is the procedure to do a custom "CFiledialog" resource?
Thanks and Happy New Year
Doc
|
|
|
|
|
This question should be posted in the Visual C++[^] forum. This is the C++/CLI forum that's dedicated to the new managed and mixed-mode compiler available in VC++ 2005.
|
|
|
|
|
Sorry
|
|
|
|
|
is it possible to publish my cpp program code in this website ? does the code have to meet some specifications/standard ? how do i submit it ?
|
|
|
|
|
thekeralite wrote: is it possible to publish my cpp program code in this website ? does the code have to meet some specifications/standard ? how do i submit it ?
Please see our Submission Guidelines[^]
|
|
|
|
|
Hello,
I am making a windowed application and need the program to sit in a loop waiting to receive messages. I am having some trouble with the thread. Here is the code I have now...
Form1 * messageThread;
messageThread = new Form1;
ThreadStart * pThread = new ThreadStart(messageThread, &Form1::messageLoop);
Thread *oThread = new Thread(pThread);
oThread->Start();
This code compiles fine, but I don't think it works correctly. This is my first experience with threads, so I would appreciate any input. This code is executed when a button is clicked. I want it to call the messageLoop function, which is in Form1. When the thread receives a message, I want it to output it to the gui - but it does not do this. Any thoughts?
Roy K
|
|
|
|
|
|
Is it possible to create a thread in the class of the function that you want to call? That sounds confusing... I want to create a thread in my Form1 class so it can call the messageLoop function, which is in the Form1 class. I thought that is what this code did,
Form1 * messageThread;
messageThread = new Form1;
ThreadStart * pThread = new ThreadStart(messageThread, &Form1::messageLoop);
Thread *oThread = new Thread(pThread);
oThread->Start();
but i was wrong...
-- modified at 18:08 Tuesday 3rd January, 2006
Scratch that...stupid mistake
the code should look like the following if I wanted a change to be visible on the gui...
ThreadStart * pThread = new ThreadStart(this, &Form1::messageLoop);
Thread *oThread = new Thread(pThread);
oThread->Start();
I used the current class instance ('this') instead of creating a new class instance. This showed the changes to the gui that was running.
thanks for the help
|
|
|
|
|
// CR - carriage return
// LF - line feed
// In "cpp.properties", any new line is set as CRLF, but when the text document has been written to "bbb.txt", CRCRLF appears in "bbb.txt" instead of CRLF. Can anyone please tell me where the problem lies in?
#include <iostream>
#include <fstream>
#include <windows.h>
typedef unsigned short int usint;
int main(usint arg, char *parameter[]) {
if (arg == 2) {
using namespace std;
ifstream::pos_type size;
char *memblock;
const char *convert;
string search = "command.build.*.cpp=M:/Dev-Cpp/bin/g++.exe -o \"$(FileName)\" \"$(FileName).$(FileExt)\"";
string link_file = " -Xlinker ";
link_file += parameter[1];
string filebuffer;
ifstream readfile ("M:/SciTE/cpp.properties", ios::in|ios::binary|ios::ate);
if (readfile.is_open()) {
size = readfile.tellg();
memblock = new char [size];
readfile.seekg (0, ios::beg);
readfile.read (memblock, size);
readfile.close();
filebuffer = memblock;
delete []memblock;
filebuffer.insert((filebuffer.find(search, 0) + 93), link_file);
ofstream writefile("M:/bbb.txt");
if (writefile.is_open()) {
writefile << filebuffer;
writefile.close();
system("M:/bbb.txt");
}
else {
MessageBox (NULL, "Unable to WRITE file.", "ERROR", MB_OK);
goto end;
}
}
else {
MessageBox (NULL, "Unable to READ file.", "ERROR", MB_OK);
goto end;
}
end: return 0;
}
}
-- modified at 20:50 Monday 2nd January, 2006
|
|
|
|