Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to creat a new folder and a new file

How to creat a new folder and a new file

Scheduled Pinned Locked Moved C / C++ / MFC
questionioshelptutorial
5 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zhangguoliming
    wrote on last edited by
    #1

    Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();

    D realJSOPR S H 4 Replies Last reply
    0
    • Z zhangguoliming

      Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      zhangguoliming wrote:

      strcat(path,m_Luting);

      This should probably be strcpy() instead.

      zhangguoliming wrote:

      fMyfile.open(path,ios:out|ios::binary|ios::trunc);

      What is the value of path at this point? On a side note, since you are using MFC, have you looked into CFile?


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      1 Reply Last reply
      0
      • Z zhangguoliming

        Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3
        1. don't put m_Luting in a _T() macro. 2) Since you're using MFC, use it all the way...

        if (CreateDirectory(m_Luting))
        {
        CString sPath;
        sPath.Format("%s\\%s%s", m_Luting, m_Name, stName);
        CFile file;
        CFileException e;
        if (file.Open(sPath, CFile::modeCreate | CFile::modeWrite, &e)
        {
        #ifdef _DEBUG
        afxDump << "File could not be opened " << e.m_cause << "\n";
        #endif
        return;
        }
        else
        {
        file.Close();
        }
        }
        else
        {
        AfxMessageBox("Could not create folder.");
        }

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        1 Reply Last reply
        0
        • Z zhangguoliming

          Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();

          S Offline
          S Offline
          Sceptic Mole
          wrote on last edited by
          #4

          zhangguoliming wrote:

          Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance.

          CDiskObject: Simplifying common disk operations[^]

          1 Reply Last reply
          0
          • Z zhangguoliming

            Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance. m_Luting is the name of the folder , type CString m_Name is the name of the file ,type CString My code : char path[300]; CString stname=".bin"; CString cell; CreateDirectory(_T(m_Luting), NULL); strcat(path,m_Luting); strcat(path,m_Name); strcat(path,stname); fstream fMyfile; fMyfile.open(path,ios:out|ios::binary|ios::trunc); if(!fMyfile){ exit(-1);} fMyfile.close();

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            You can also use of CreateFile for create or open a file


            WhiteSky


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups