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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CStdioFile save location to wrong path [modified]

CStdioFile save location to wrong path [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadminhelp
2 Posts 2 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.
  • A Offline
    A Offline
    acerunner316
    wrote on last edited by
    #1

    I'm trying to save a file to a fixed location. The would like file to be saved in the same folder as the executable, so I am using a relative path. However, I get mixed results. Sometimes it saves to the proper folder, sometimes to desktop, and sometimes to C:\. If I use an absolute path, the problem goes away. So my question is whether the path specified when opening the file is required to be absolute? And if so, how can I get an absolute path from a relative path not knowing where the executable will be installed? Here is my code:

    CStdioFile LcfIniFile;
    CString LcsTempStr;

    if (LcfIniFile.Open("test.ini", CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeText | CFile::shareDenyWrite))
    {
    //Process data to be writting to file in LcsTempStr
    ...
    LcfIniFile.WriteString(LcsTempStr);
    LcfIniFile.Close();
    }

    Thanks. Edit: MSDN says path can be relative, absolute, or network name. So now I really don't know what I'm doing wrong. Edit2: I should also note that reading the file using the relative path did not cause any problems, it was only during write. -- modified at 15:49 Wednesday 24th October, 2007

    P 1 Reply Last reply
    0
    • A acerunner316

      I'm trying to save a file to a fixed location. The would like file to be saved in the same folder as the executable, so I am using a relative path. However, I get mixed results. Sometimes it saves to the proper folder, sometimes to desktop, and sometimes to C:\. If I use an absolute path, the problem goes away. So my question is whether the path specified when opening the file is required to be absolute? And if so, how can I get an absolute path from a relative path not knowing where the executable will be installed? Here is my code:

      CStdioFile LcfIniFile;
      CString LcsTempStr;

      if (LcfIniFile.Open("test.ini", CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeText | CFile::shareDenyWrite))
      {
      //Process data to be writting to file in LcsTempStr
      ...
      LcfIniFile.WriteString(LcsTempStr);
      LcfIniFile.Close();
      }

      Thanks. Edit: MSDN says path can be relative, absolute, or network name. So now I really don't know what I'm doing wrong. Edit2: I should also note that reading the file using the relative path did not cause any problems, it was only during write. -- modified at 15:49 Wednesday 24th October, 2007

      P Offline
      P Offline
      Peter Weyzen
      wrote on last edited by
      #2

      I think it all depends on what the current directory is. And that all depends on how you run it.... Why not construct an explicit path to the file -- that way you can be sure. use GetModuleFileName to determine the path of the exe... split off the name and add "test.ini"

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

      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