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 can't believe I am having so many problems with file access

I can't believe I am having so many problems with file access

Scheduled Pinned Locked Moved C / C++ / MFC
helpios
6 Posts 3 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.
  • L Offline
    L Offline
    ldsdbomber
    wrote on last edited by
    #1

    I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!

    L T D 3 Replies Last reply
    0
    • L ldsdbomber

      I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!

      L Offline
      L Offline
      ldsdbomber
      wrote on last edited by
      #2

      just so you don't think I am not writing anything, here is what I've got so far, please do not laugh void CTestFolderBrowseDlg::OnSaveentry() { // TODO: Add your control notification handler code here int sel = m_Table.GetSelectionMark(); if(sel < 0) return; // ERROR no selection // Build Target File Name if(!pathLoaded) return; // ERROR no path info CString energyText; energyText = m_Table.GetItemText(sel, 0); CString fileName(path); fileName += '\\'; if(energyText.GetLength()<2) fileName += "0"; energyText += "MeV.dat"; fileName += energyText; // Check if File Exists fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); // note, the noreplace flag doesnt work if(FileDataStream.is_open()) { // file exists (I think without noreplace, it's always going to end up in here) // Read Data // Append Data FileDataStream<

      1 Reply Last reply
      0
      • L ldsdbomber

        I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        just curious : why are you creating a fstream if you only need to write to the file ? doesn't a ofstream suffice ?


        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        1 Reply Last reply
        0
        • L ldsdbomber

          I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!

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

          ldsdbomber wrote:

          I've only looked in the help and checked out "fstream"...

          But you did not look in ios.h, which is included by fstream.h. With the newer header files, the noreplace and nocreate flags were removed.

          ldsdbomber wrote:

          - check if a file (specified by a CString object) exists.

          How about _access(str, 0)? Or just try to open the file for reading. If that fails, the file likely does not exist.

          ldsdbomber wrote:

          - if it does, I want to nip to the end of the file and start appending data rows

          Have you tried ios::app?


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          L 1 Reply Last reply
          0
          • D David Crow

            ldsdbomber wrote:

            I've only looked in the help and checked out "fstream"...

            But you did not look in ios.h, which is included by fstream.h. With the newer header files, the noreplace and nocreate flags were removed.

            ldsdbomber wrote:

            - check if a file (specified by a CString object) exists.

            How about _access(str, 0)? Or just try to open the file for reading. If that fails, the file likely does not exist.

            ldsdbomber wrote:

            - if it does, I want to nip to the end of the file and start appending data rows

            Have you tried ios::app?


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            L Offline
            L Offline
            ldsdbomber
            wrote on last edited by
            #5

            thanks David. I have not used _access before, I am mainly relying on the help files within VC from pressing F1 with the keyword highlighted or searching in the index there. It doesn't always seem very well organised and I am often confused about when I am looking at pure C, C++, MFC, STL etc etc. But _access seems to be exactly what I am looking for. If you can recommend a good book that covers all this newer up to date stuff, that would be great. For the most part, I'm looking at MFC dialog apps

            D 1 Reply Last reply
            0
            • L ldsdbomber

              thanks David. I have not used _access before, I am mainly relying on the help files within VC from pressing F1 with the keyword highlighted or searching in the index there. It doesn't always seem very well organised and I am often confused about when I am looking at pure C, C++, MFC, STL etc etc. But _access seems to be exactly what I am looking for. If you can recommend a good book that covers all this newer up to date stuff, that would be great. For the most part, I'm looking at MFC dialog apps

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

              ldsdbomber wrote:

              For the most part, I'm looking at MFC dialog apps

              For MFC, you can use CFile::GetStatus(). There's also GetFileAttributes().


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              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