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. I have an error

I have an error

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
7 Posts 4 Posters 0 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.
  • V Offline
    V Offline
    vhazell
    wrote on last edited by
    #1

    Hi all !! I practise VC. When I code my program to create a file. The first time when I click my button2 (create file) I receive a error "access pathdir to file is denied" but I can create file at the second click. I don't know why ??? Please,I need help !! This's my code when click button2, I use MFC wizard (use dialogbase) void CTestDlg::OnButton2() { // TODO: Add your control notification handler code here char filename[100]; GetCurrentDirectory(100,filename); strcat(filename,"\\RongVang.dat"); CFile f; if(!f.Open(filename,CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); f.SeekToEnd(); char s[100]; strcpy(s,"Test Create file"); f.Write(s,100); } -- modified at 22:36 Sunday 16th October, 2005

    G P 2 Replies Last reply
    0
    • V vhazell

      Hi all !! I practise VC. When I code my program to create a file. The first time when I click my button2 (create file) I receive a error "access pathdir to file is denied" but I can create file at the second click. I don't know why ??? Please,I need help !! This's my code when click button2, I use MFC wizard (use dialogbase) void CTestDlg::OnButton2() { // TODO: Add your control notification handler code here char filename[100]; GetCurrentDirectory(100,filename); strcat(filename,"\\RongVang.dat"); CFile f; if(!f.Open(filename,CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); f.SeekToEnd(); char s[100]; strcpy(s,"Test Create file"); f.Write(s,100); } -- modified at 22:36 Sunday 16th October, 2005

      G Offline
      G Offline
      GflPower
      wrote on last edited by
      #2

      the filename return format is not like this"C:\\ adir" it return format is "C:\adir" it can't use as a parameter in F.Open Function . d

      V 1 Reply Last reply
      0
      • G GflPower

        the filename return format is not like this"C:\\ adir" it return format is "C:\adir" it can't use as a parameter in F.Open Function . d

        V Offline
        V Offline
        vhazell
        wrote on last edited by
        #3

        Thank so much !! But how can i slove it .... Can you show me ..... I've just program...

        1 Reply Last reply
        0
        • V vhazell

          Hi all !! I practise VC. When I code my program to create a file. The first time when I click my button2 (create file) I receive a error "access pathdir to file is denied" but I can create file at the second click. I don't know why ??? Please,I need help !! This's my code when click button2, I use MFC wizard (use dialogbase) void CTestDlg::OnButton2() { // TODO: Add your control notification handler code here char filename[100]; GetCurrentDirectory(100,filename); strcat(filename,"\\RongVang.dat"); CFile f; if(!f.Open(filename,CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); f.SeekToEnd(); char s[100]; strcpy(s,"Test Create file"); f.Write(s,100); } -- modified at 22:36 Sunday 16th October, 2005

          P Offline
          P Offline
          P Rex
          wrote on last edited by
          #4

          Why do you open the file two times? i think there you get the error. if(!f.Open(filename,CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); Try this: if(!f.Open(filename,CFile::modeCreate | CFile::modeReadWrite))

          V 1 Reply Last reply
          0
          • P P Rex

            Why do you open the file two times? i think there you get the error. if(!f.Open(filename,CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); Try this: if(!f.Open(filename,CFile::modeCreate | CFile::modeReadWrite))

            V Offline
            V Offline
            vhazell
            wrote on last edited by
            #5

            if(!f.Open(filename,CFile::modeCreate | CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); The first click it will create file. I want at the second click, third,4,5,.... click in button2, it will append my file, not delete and create new file like above !! Can you help me ... !!!!!!!!!!!!

            P D 2 Replies Last reply
            0
            • V vhazell

              if(!f.Open(filename,CFile::modeCreate | CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); The first click it will create file. I want at the second click, third,4,5,.... click in button2, it will append my file, not delete and create new file like above !! Can you help me ... !!!!!!!!!!!!

              P Offline
              P Offline
              P Rex
              wrote on last edited by
              #6

              CString filename = _T("C:\\RongVang.txt"); CStdioFile f; if(!f.Open(filename,CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate|CFile::modeReadWrite); f.SeekToEnd(); CString s = _T("Test Create file\n"); f.WriteString(s); dont forget to close your file.

              1 Reply Last reply
              0
              • V vhazell

                if(!f.Open(filename,CFile::modeCreate | CFile::modeReadWrite)) f.Open(filename,CFile::modeCreate); The first click it will create file. I want at the second click, third,4,5,.... click in button2, it will append my file, not delete and create new file like above !! Can you help me ... !!!!!!!!!!!!

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

                vhazell wrote:

                I want at the second click, third,4,5,.... click in button2, it will append my file, not delete and create new file like above !!

                Then use:

                if (f.Open(filename, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate) != FALSE)
                {
                f.SeekToEnd();
                ...
                }


                "Take only what you need and leave the land as you found it." - Native American Proverb

                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