|
_Pinux_ wrote: Pretty ugly. Isn't there a way to let the control and its owner both receive the same notification?
No
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I see. Thanks.
Toe the line
Thread the needle
Think outside of the box
|
|
|
|
|
Hello Guru Jiiiiiiiiii's
I am making a application in VC++(MFC-Modal Dialog based).And i don't know
file handling ........The question i am writing here i have already post here
but I am sending my code also. my question is below . Plz help me
Actually I am making a file field ( Mense when user click browse button
all HD display and he/she can attach his .txt document ) That .txt doc
will be save at a file which is created in C:Drive in a folder ( it can any other place also )
I have done its Most of work like i can read file create new file also
but i am unable to write that data in to newly created file.
I don't know where i am making wrong . I know its vewry basic . But i am also know i am also very begginner in file handling
My whole code That i have write for this is below
CString str;
int ids;
CFileDialog Obj(TRUE);
if(Obj.DoModal() ==IDC_BUTTON3)
{
m_Upload = Obj.GetFileName();
}
str.Empty();
str = Obj.GetPathName();
SetDlgItemText(IDC_EDIT1,str); //write file path with name at edit box
CString strLine = "";
CString buffer = "";
CStdioFile fileObj,filewrite;
CString fname;
fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite);
fname = fileObj.GetFileName();
while(fileObj.ReadString(strLine))
{
fileObj.ReadString(strLine);
buffer += strLine + "\r\n";
}
fileObj.Close();
// MessageBox(buffer);
SetDlgItemText(IDC_EDIT3,buffer);////write all data at another edit box
fileObj.Open("c:\\UserFolder\\" + fname + ".txt",CFile::modeCreate,NULL);
CString dataStr;
GetDlgItemText(IDC_EDIT3,dataStr);
CStdioFile myFile;
CFileException e;
CString strFileName =str;
if(! myFile.Open( strFileName, CStdioFile::modeCreate | CStdioFile::modeWrite
| CStdioFile::modeNoTruncate, &e ) )
{
CString failCause = "Unable to open file. Reason : ";
switch (e.m_cause) {
case CFileException::none:
failCause += "No error occurred.";
break;
case CFileException::generic:
failCause += "An unspecified error occurred.";
break;
case CFileException::fileNotFound:
failCause += "The file could not be located.";
break;
case CFileException::badPath:
failCause += "All or part of the path is invalid.";
break;
case CFileException::tooManyOpenFiles:
failCause += "The permitted number of open files was exceeded.";
break;
case CFileException::accessDenied:
failCause += "The file could not be accessed.";
break;
case CFileException::invalidFile:
failCause += "There was an attempt to use an invalid file handle.";
break;
case CFileException::removeCurrentDir:
failCause += "The current working directory cannot be removed.";
break;
case CFileException::directoryFull:
failCause += "There are no more directory entries.";
break;
case CFileException::badSeek:
failCause += "There was an error trying to set the file pointer.";
break;
case CFileException::hardIO:
failCause += "There was a hardware error.";
break;
case CFileException::sharingViolation:
failCause += "SHARE.EXE was not loaded, or a shared region was locked.";
break;
case CFileException::lockViolation:
failCause += "There was an attempt to lock a region that was already locked.";
break;
case CFileException::diskFull:
failCause += "The disk is full.";
break;
case CFileException::endOfFile:
failCause += "The end of file was reached.";
break;
default:
failCause += "Unknown cause.";
break;
}
AfxMessageBox(failCause);
}
else
{
myFile.SeekToEnd();
CString strLine = dataStr;
strLine += "\r\n";
myFile.WriteString(strLine);
// myFile.WriteString(dataStr);
myFile.Close();
}
I write that all code with the help of net also its work almost fine But
The main task i.e write all data at file in userfolder is not perform
Please told me where i am wrong
Regard's
Kaushik
|
|
|
|
|
Can you explain about this line "if(Obj.DoModal() ==IDC_BUTTON3)".
|
|
|
|
|
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
It mense When particular button which id is (IDC_BUTTON3) clicked
Then event fire and Browse the window for selecting your file
Regard's
Kaushik
|
|
|
|
|
Have you got any error message? If the answer is Yes then, please, post it.
BTW, the debugger is your best frined.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
I don't have any error message
But data is not write at file
Regard's
Kaushik
|
|
|
|
|
myFile and fileObj are use mixed, I think you neednt myFile. Really
Greetings from Germany
|
|
|
|
|
Your code has some problem
(1)
if(Obj.DoModal() ==IDC_BUTTON3)
to
if(Obj.DoModal() ==IDOK)
(2)
while(fileObj.ReadString(strLine))
{
fileObj.ReadString(strLine);
buffer += strLine + "\r\n";
}
to
while(fileObj.ReadString(strLine))
{
buffer += strLine + "\r\n";
}
fileObj.ReadString(strLine);
(3)
Do you have this folder on your system
fileObj.Open("d:\\UserFolder\\" + fname + ".txt",CFile::modeCreate,NULL);
(4)
if(! myFile.Open( strFileName
, CStdioFile::modeCreate | CStdioFile::modeWrite
| CStdioFile::modeNoTruncate, &e ) )
and also you have an if/else block
|
|
|
|
|
Do you have this folder on your systemfileObj.Open("d:\\UserFolder\\" + fname + ".txt",CFile::modeCreate,NULL);
Yes This folder in my system
I have replace all change but problem remain
Regard's
Kaushik
|
|
|
|
|
Can any move to solve this problem.
After all change I am gotted any solution ...
Please advise me for solving this;)
Regard's
Kaushik
|
|
|
|
|
You dont have any code for write on the if block you have a write to file on the else block.
|
|
|
|
|
I think first time i have seen when WhiteSky has given the code directly instead of link.
Well, I have nothing to do with it. It's good that you are replying and helping the programmers in any way you can. Good.
|
|
|
|
|
Anurag Gandhi wrote: I think first time i have seen when WhiteSky has given the code directly instead of link.
Don't you like links, do you?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Oh no its not first time it seems to question for example on this question a link arent helpful for asker because he wrote his code but needs to help but for some question if we see an article is better for asker.;)
|
|
|
|
|
Below is the code to save the file that i am using to save the text file.
I hope you will understand each line of it. It is very easy.
CStdioFile file;
if(!file.Open(lpszPathName,CFile::modeCreate | CFile::modeWrite, 0))
{
AfxMessageBox(CString("File cannot be Saved. Please Check if the file is read only."));
return FALSE;
}
long nLen = reinterpret_cast<ceditview*>(m_viewList.GetHead())->GetWindowTextLengthW();
TCHAR* pSaveText = new TCHAR[nLen+1];
reinterpret_cast<ceditview*>(m_viewList.GetHead())->GetWindowText(pSaveText,nLen + 1);
file.WriteString(pSaveText);
file.Close();
|
|
|
|
|
Can u told me in your code
AfxMessageBox(CString("File cannot be Saved. Please Check if the file is read only."));
return FALSE;
When function return type void how we return FALSE ?
Second
What is m_viewList ?? I think its mamber variable of List
What is pSaveText ?? I think Its data which I have to read from file
Regard's
Kaushik
|
|
|
|
|
I am simplifying my answer according to your need.
CStdioFile file;
if(!file.Open(lpszPathName,CFile::modeCreate | CFile::modeWrite, 0))
{
AfxMessageBox(CString("File cannot be Saved. Please Check if the file is read only."));
return;
}
CString strTextToSave = _T("This text will be saved to the file.");
file.WriteString(strTextToSave);
file.Close();
Hope it is clear.
For Your Information:
The code i have given you was from one of my project where I called FileSave function in CProjectDocument Class and was retriving the text from EditView Window.
|
|
|
|
|
Dear Sir
I have write this code for write data on file I can Create file in my user folder but Nothing data is write
Can u give me any correct solution for this OR Told me where I am wrong
CString str;
int ids;
CFileDialog Obj(TRUE);
if(Obj.DoModal() ==IDC_BUTTON3)
{
m_Upload = Obj.GetFileName();
}
str.Empty();
str = Obj.GetPathName();
SetDlgItemText(IDC_EDIT1,str); //write file path with name at edit box
CString strLine = "";
CString buffer = "";
CStdioFile fileObj;
CString fname;
fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite);
fname = fileObj.GetFileName();
while(fileObj.ReadString(strLine))
{
buffer += strLine + "\r\n";
}
fileObj.Close();
// MessageBox(buffer);
SetDlgItemText(IDC_EDIT3,buffer);////write all data at another edit box
fileObj.Open("c:\\UserFolder\\" + fname + ".txt",CFile::modeCreate,NULL);
CString dataStr;
GetDlgItemText(IDC_EDIT3,dataStr);
CStdioFile file;
if(!file.Open(str,CFile::modeCreate | CFile::modeWrite, 0))
{
AfxMessageBox(CString("File cannot be Saved. Please Check if the file is read only."));
return;
}
CString strTextToSave = _T("This text will be saved to the file.");
file.WriteString(dataStr);
file.Close();
Regard's
Kaushik
|
|
|
|
|
In second last line:
<br />
file.WriteString(dataStr);<br />
You didn't assign any value to dataStr; The default value is empty string that's why you are getting empty string.
|
|
|
|
|
First off all Thanks for your reply
secondly
Sir I think u r not gotted my exact problem. Sir Ji I have my text data
in "dataStr" which i want to save at that file whatever i have create.
According to code I am trying to do this...
When user click browse button whole HD show The user select any one file for saving it in to another folder. [Its work something like File field in HTML ]
Then
Suppose I have select a file D:\Test\document.txt. Now
I have create a file name "document.txt" in UserFolder in C Drive
Now I want to save all contant(data) of D:\Test\document.txt in to
C:\UserFolder\document.txt
Till Now I can create a file in C:\UserFolder\document.txt
but can not write data from D:\Test\document.txt to C:\UserFolder\document.txt I found file in C drive Empty
Can u solve this problem If so ............Please
Regard's
Kaushik
|
|
|
|
|
Debug your application and check where the priblem is.
or
Try something like this: And be sure not to use fileObj.Close() before the following code:
fileObj.SeekToBegin();
while(fileObj.ReadString(strLine))
{
strLine += "\r\n";
buffer += strLine;
file.WriteString(strLine);
}
file.Close();
fileObj.Close();
This is reading one file line by line and writing to another file.
|
|
|
|
|
sir
I am writing this code
CStdioFile file;
if(!file.Open(str,CFile::modeCreate | CFile::modeWrite, 0))
{
AfxMessageBox(CString("File cannot be Saved. Please Check if the file is read only."));
return;
}
file.SeekToBegin();
while(file.ReadString(buffer))
{
buffer += "\r\n";
strLine+= buffer;
file.WriteString(buffer);
}
file.Close();
fileObj.Close();
Program comlpile successfully But when i run it Its Show
File cannot be Saved. Please Check if the file is read only
Message . Can u told me where need to change
Regard's
Kaushik
|
|
|
|