CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2020
    Posts
    2

    Problem with function rename()

    Hello to all. I'm new in this forum. I'm writing a simple function (using MFC) that open a folder, write in a file .txt and rename the folder. This is the code snippet:
    Code:
        CString s, s1, s2;
    
        s.Format("C:\\MyFolder\\test.txt");
        CStdioFile cf1;
        if (!(cf1.Open(s, CFile::modeWrite | CFile::modeCreate | CFile::typeText))) 
           { 
                      MessageBox("Error ", "Attention"); return; 
            }
        
        cf1.WriteString("OK\n");
        cf1.Close();
    
        s1.Format("C:\\MyFolder");
        s2.Format("C:\\MyFolderRen");
    
       result = rename(s1, s2);
       if (result == -1)
           {
    		s1.Format("%s", strerror(errno));  //"Permission denied"
    	}
    rename() function doesn't rename my folder if before I wrote in a file conteined in that folder.
    The value returned is -1.
    Obviously the function works correctly if I only rename that folder and I don't write into the file. What is the reason of this behavior?
    Thanks in advance.
    Last edited by Arjay; December 10th, 2020 at 10:45 PM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,495

    Re: Problem with function rename()

    Probably CStdioFile is holding onto its underlying file handle.

    Try scoping it with curly braces.

    Code:
        CString s, s1, s2;
    
        s.Format("C:\\MyFolder\\test.txt");
        {
            CStdioFile cf1;
            if (!(cf1.Open(s, CFile::modeWrite | CFile::modeCreate | CFile::typeText))) 
            { 
                  MessageBox("Error ", "Attention"); return; 
             }
        
            cf1.WriteString("OK\n");
            cf1.Close();
        }
        s1.Format("C:\\MyFolder");
        s2.Format("C:\\MyFolderRen");
    
       result = rename(s1, s2);
       if (result == -1)
       {
            s1.Format("%s", strerror(errno));  //"Permission denied"
        }

  3. #3
    Join Date
    Dec 2020
    Posts
    2

    Re: Problem with function rename()

    Now it works, thank you.
    I tried to extend this concept into a slightly more complex function, but in this way the function rename() still returns -1. The function simply checks inside a folder.
    If there is a file in the root folder, the function opens the source file and copies its contents into a temporary file, replacing only a line of the source file (my program has two edit boxes which rappresent the name to find and the name to replace). The source file will then be deleted and the temporary file will be renamed with the name of the source file.
    If there is a subfolder in the root folder, the function checks inside that subfolder and essentially it does the same operations of copy and renaming that I've explained above.
    The code is the sequent:

    Code:
    void CCambiaNomiArchivioDlg::OnBnClickedConverti()
    {
    	
    	HCURSOR hCur1, hCur2;
    	CString s, s1, s2, path1, path2, pathTipi, lotto, serie;
    	char bb[1000], bb1[1000];
    	int result;
    
            dir_root.Format("C:\\Folder");
    	pathTipi.Format("%s\\Tipi", dir_root);
    
    	GetDlgItem(IDC_EDIT1)->GetWindowText(m_strNomeorig);
    	m_strNomeorig.Trim();
    	GetDlgItem(IDC_EDIT2)->GetWindowText(m_strNomemod);
    	m_strNomemod.Trim();
    
    	if (m_strNomeorig == "")  { MessageBox("Error in source name", "Attention"); return; }
    	else if (m_strNomemod == "") { MessageBox("Error in mod name", "Attention"); return; }
    
    	path1.Format("%s\\Tipi\\%s\\*", dir_root, m_strNomeorig);
    	strcpy(bb, path1.GetBuffer(path1.GetLength()));
    
    	{
    		WIN32_FIND_DATA fd1;
    		HANDLE fh1 = FindFirstFile(bb, &fd1);
    
    		hCur1 = LoadCursor(NULL, IDC_WAIT);
    		hCur2 = LoadCursor(NULL, IDC_ARROW);
    
    		if (fh1 == INVALID_HANDLE_VALUE)
    		{
    			MessageBox("There isn't a file at this path", "Attention");
    			return;
    		}
    		else
    		{
    			SetCursor(hCur1);
    
    			lotto.Format("%s", fd1.cFileName);
    			FindNextFile(fh1, &fd1);
    			lotto.Format("%s", fd1.cFileName);
    			while (FindNextFile(fh1, &fd1))
    			{
    				lotto.Format("%s", fd1.cFileName);
    				if (strcmp(lotto, _T("AAA.dat")) == 0)
    				{
    					s.Format("%s\\Tipi\\%s\\%s", dir_root, m_strNomeorig, fd1.cFileName);
    					s1.Format("%s\\Tipi\\%s\\%s", dir_root, m_strNomeorig, _T("temp.txt"));
    					CStdioFile cf1, cf2;
    					if (!(cf1.Open(s, CFile::modeRead | CFile::typeText))) 
                                            { MessageBox("Impossible to open AAA.dat", "Attention"); return; }
    					if (!(cf2.Open(s1, CFile::modeWrite | CFile::modeCreate | CFile::typeText))) 
                                            { MessageBox("Impossible to open temp.dat", "Attention"); return; }
    
    					while (cf1.ReadString(s2))
    					{
    						s2.Trim();
    						if (s2 == m_strNomeorig) s2.Format("%s", m_strNomemod);
    						cf2.WriteString(s2 + "\n");
    					}
    
    					cf1.Close();
    					cf2.Close();
    
    					remove(s);
    					rename(s1, s);
    				}
    				else
    				{
    					path2.Format("%s\\Tipi\\%s\\%s\\*", dir_root, m_strNomeorig, lotto);
    					strcpy(bb1, path2.GetBuffer(path2.GetLength()));
    					WIN32_FIND_DATA fd2;
    					HANDLE fh2 = FindFirstFile(bb1, &fd2);
    					serie.Format("%s", fd2.cFileName);
    					FindNextFile(fh2, &fd2);
    					serie.Format("%s", fd2.cFileName);
    					while (FindNextFile(fh2, &fd2))
    					{
    						serie.Format("%s", fd2.cFileName);
    						s.Format("%s\\Tipi\\%s\\%s\\%s", dir_root, m_strNomeorig, lotto, serie);
    						s1.Format("%s\\Tipi\\%s\\%s\\%s", dir_root, m_strNomeorig, lotto, "temp.txt");
    						CStdioFile cf1, cf2;
    						if (!(cf1.Open(s, CFile::modeRead | CFile::typeText))) 
                                                    { MessageBox("Impossible to open Serie file", "Attention"); return; }
    						if (!(cf2.Open(s1, CFile::modeWrite | CFile::modeCreate | CFile::typeText))) 
                                                    { MessageBox("Impossible to open temp file", "Attention"); return; }
    
    						while (cf1.ReadString(s2))
    						{
    							s2.Trim();
    							if (s2 == m_strNomeorig) s2.Format("%s", m_strNomemod);
    							cf2.WriteString(s2 + "\n");
    						}
    
    						cf1.Close();
    						cf2.Close();
    
    						remove(s);
    						rename(s1, s);
    
    					}
    				}
    			}
    		}
    		SetCursor(hCur2);
    	}
    
    	s1.Format("%s\\%s", pathTipi, m_strNomeorig);
    	s2.Format("%s\\%s", pathTipi, m_strNomemod);
    	rename(s1, s2);                                                //This renaming returns -1 
    }
    Last edited by 2kaud; December 15th, 2020 at 07:28 AM. Reason: Added code tags

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,624

    Re: Problem with function rename()

    Quote Originally Posted by Arjay View Post
    Probably CStdioFile is holding onto its underlying file handle.

    Try scoping it with curly braces.

    Code:
        CString s, s1, s2;
    
        s.Format("C:\\MyFolder\\test.txt");
        {
            CStdioFile cf1;
            if (!(cf1.Open(s, CFile::modeWrite | CFile::modeCreate | CFile::typeText))) 
            { 
                  MessageBox("Error ", "Attention"); return; 
             }
        
            cf1.WriteString("OK\n");
            cf1.Close();
        }
        s1.Format("C:\\MyFolder");
        s2.Format("C:\\MyFolderRen");
    
       result = rename(s1, s2);
       if (result == -1)
       {
            s1.Format("%s", strerror(errno));  //"Permission denied"
        }
    This code worked for me, after defining result. Perhaps you just had File Manager looking inside that folder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured