질문하기질문하기
 

답변됨C++ -- saveFileDialog

  • 2009년 11월 4일 수요일 오후 9:27ab00120 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     코드 있음
    Hi there,

    Having posted in the C++ part of the forum I have been directed over to the .NET Base class Library.  Below is my original post and my really poor attempt at implementing the advice given.  I am really quite struggling to get to grips with this so any help is much appreciated.


    "Hi there,

    I am currently attempting to enhance an old program to use Windows Forms in C++.  From what Ive read it would be preferable to use Visual Basic or C#; however in its current state, combined with my lack of experience in C#/VB this would be a real struggle to attempt at this stage.

    Hopefully by outlining an example it will be possible to convey my current issue.

    Essentially I have opened a string stream and passed its contents into a std::string.  Originally this was to be output in the console screen.  By implementing Windows forms in my programme I now wish to create a button that when clicked opens the saveFileDialog allowing me to save this string as a text file.

    This is where I curently am at:

    //string.h
    #include <iostream>
    #include <sstream>
    
    class test
    {
    public:
    	std::string write();
    };
    
    //string.cpp
    #include "stdafx.h"
    #include "string.h"
    
    std::string test::write()
    {
    	std::ostringstream output;
    	output << "HELLO" << std::endl;
    	output << "HELLO AGAIN" << std::endl;
    	std::string s2 = output.str();
    	return s2;
    }
    
    //Form1.h
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e){
    			 test a;
    			 SaveFileDialog ^ saveFileDialog1 = gcnew SaveFileDialog();
    			 saveFileDialog->Filter = "Text file (*.txt)|*.txt";
    			 saveFileDialog->Title = "Save a text file";
    
    			 //CODE REQUIRED TO SAVE s2 AS .TXT FILE
    
    			 saveFileDialog1->ShowDialog();
    		 }
    
    I hope it can be seen that I have a seperate string.ccp and corresponding header file to define the string contents which is a requirement of my programme.

    Unless what I have done is flawed in some way I think all I need is a point in the direction as to what code to place in Form1.h as there is very little information around the internet for this sort of thing.

    One piece that I did find used fsteam instead of sstrem though im not sure if this would make a significant difference?

    Thank you very much in advance for looking at this, your time is much appreciated.

    Andy"

    This was my next step but as far as I am aware, Im missing the code for the next step which I really cant figure out:
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e){
    			 test a;
    			 SaveFileDialog ^ saveFileDialog1 = gcnew SaveFileDialog();
    			 saveFileDialog->Filter = "Text file (*.txt)|*.txt";
    			 saveFileDialog->Title = "Save a text file";
    			 saveFileDialog1->ShowDialog();
    
    			 if (result == System::Windows::Forms::DialogResult::OK)
    			 {
    				string b = a.write();
    				///lost
    			}
    
    		 }
    		 
    
    Once again thank you for all your help/

답변

  • 2009년 11월 12일 목요일 오전 5:44mpturn 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    So you are already able to open a save file dialog?  If so, then in your if(result == DialogResult::OK) statement, you can access the filename that was chosen for saving as saveFileDialog1->FileName, and then save your string to this file using a StreamWriter .

    And for what it's worth, I second the notion that you should use c# rather than c++, no matter how much of a struggle you may anticipate.  As someone who learned how to program using c/c++ on Unix, it pains me to see the extensive Microsoftication of the language present in Visual C++ (gcnew, ^, etc.).  Rather than trying to force-feed modern programming paradigms to an antiquated language, why not just start fresh with a programming language which is designed from the ground up for modern applications?

모든 응답

  • 2009년 11월 12일 목요일 오전 5:44mpturn 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    So you are already able to open a save file dialog?  If so, then in your if(result == DialogResult::OK) statement, you can access the filename that was chosen for saving as saveFileDialog1->FileName, and then save your string to this file using a StreamWriter .

    And for what it's worth, I second the notion that you should use c# rather than c++, no matter how much of a struggle you may anticipate.  As someone who learned how to program using c/c++ on Unix, it pains me to see the extensive Microsoftication of the language present in Visual C++ (gcnew, ^, etc.).  Rather than trying to force-feed modern programming paradigms to an antiquated language, why not just start fresh with a programming language which is designed from the ground up for modern applications?