Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have the following code

CString str1;
CFileDialog   FileDlg1(FALSE," ","ch1",OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , "TXT File(*.txt)|*.txt|All(*.*)|*.*|");
if(FileDlg1.DoModal()==IDOK)
{
    ofstream ofs(FileDlg1.GetPathName());
    ofs.write(str1,str1.GetLength());
    ofs.close();
    AfxMessageBox("CH1 Data Saved!");
}

ofstream Data_ch1("ch1.txt", ios::out);


what it simply does, it saves the data of Data_ch1 in a txt file. the problem is that it saves the data with the name ch1.txt and if this name is changed the data will be missed in the file. i need a method to change the name of the file so the name inside " " is changed depends on the users.

What I have tried:

I tried to use GetWindowText and SetWindowText but they did not work.
Posted
Updated 1-Aug-18 5:10am

1 solution

Instead of using the hard coded value "ch1.txt", use FileDlg1.GetPathName(). That returns the name of the file selected by the user.

/ravi
 
Share this answer
 
Comments
Baderd94 1-Aug-18 11:43am    
Thank you so Much, it Worked and that is what i did

// TODO: Add your command handler code here

CString str1;
CFileDialog FileDlg1(FALSE," ","ch1.txt",OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , "TXT File(*.txt)|*.txt|All(*.*)|*.*|");
if(FileDlg1.DoModal()==IDOK)
{
ofstream ofs(FileDlg1.GetPathName());
ofs.write(str1,str1.GetLength());
ofs.close();
AfxMessageBox("CH1 Data Saved!");
}

ofstream Data_ch1(FileDlg1.GetPathName(), ios::out);
Ravi Bhavnani 1-Aug-18 11:59am    
Please "accept" my solution if it worked for you. This makes it easier for others to find answers to similar questions. Thanks.

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900