Opening a file to write to
-
Hi, Its me again, looking for more help. I'm still working on the same project as last time i was on here (sending information through the ethernet) ;P The app has a main window that just displays the menu bar, and 3 dialogs. My app makes a message using the 3 different dialog windows. Each one will add a value to a number that i have when a "create" button is pressed (each one has their own button). Here is where I am having the problem. Sometimes i need to send it to the server, but some other times i need to save it to a file. So the user can make a entire file of different messages. I am using the call
CFileDialog InputFile(TRUE, "*.txt", "Message, NULL, "*.txt", NULL);
to make the window for the user to enter the name of the text file. My question is: 1)When do i create, open, close the file that I am going to be writing to? 2)Where do i do all the "ofstream out" stuff? When i click the "create" button on the 3 dialogs that make the message? Thanks for helping me. (I'm new to coding, i'm 15. Can you please keep the answer simple and with examples if you have time. If not just explain as you normally would) Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
Hi, Its me again, looking for more help. I'm still working on the same project as last time i was on here (sending information through the ethernet) ;P The app has a main window that just displays the menu bar, and 3 dialogs. My app makes a message using the 3 different dialog windows. Each one will add a value to a number that i have when a "create" button is pressed (each one has their own button). Here is where I am having the problem. Sometimes i need to send it to the server, but some other times i need to save it to a file. So the user can make a entire file of different messages. I am using the call
CFileDialog InputFile(TRUE, "*.txt", "Message, NULL, "*.txt", NULL);
to make the window for the user to enter the name of the text file. My question is: 1)When do i create, open, close the file that I am going to be writing to? 2)Where do i do all the "ofstream out" stuff? When i click the "create" button on the 3 dialogs that make the message? Thanks for helping me. (I'm new to coding, i'm 15. Can you please keep the answer simple and with examples if you have time. If not just explain as you normally would) Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
The CFileDialog does not create the file, nor does it open it. All the dialog does is give you a standard interface for choosing a file. You have to create the file afterwards. First you construct the dialog. When you call DoModal(), the dialog appears. When the user clicks open, the dialog returns IDOK. At this point your code can access the path to the file selected by the user using the GetPathName() member function.
CFileDialog InputDlg(TRUE, "*.txt", "Message.txt", NULL, "Text Files (*.txt)|*.txt", NULL); if(IDOK==InputDlg.DoModal()){ CString filename = InputDlg.GetPathName(); //create the file here }
I suggest you have a look at the documentation for CFileDialog on the MSDN. It will tell you about some other member functions you can use and also about a little error with the file filter you had in the constructor. Lorenz Prem Microsoft Corporation