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 can I open a file without typing in the full path?

How can I open a file without typing in the full path?

Scheduled Pinned Locked Moved C / C++ / MFC
questionioshelp
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.
  • E Offline
    E Offline
    Erich Ruth
    wrote on last edited by
    #1

    I open a file for reading by typing: ofstream datain("c:\\my_program\\data\\my_file.txt", ios::in); The problem is, when I give the program to some people, they have the data on the c: drive while others have data on the d: drive or something different. Is there any way of opening this file without specifying the drive? I tried typing: "..\\my_program\\data\\my_file.txt" as well as "../my_program/data/my_file.txt" but nothing has worked yet!! Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)

    P 1 Reply Last reply
    0
    • E Erich Ruth

      I open a file for reading by typing: ofstream datain("c:\\my_program\\data\\my_file.txt", ios::in); The problem is, when I give the program to some people, they have the data on the c: drive while others have data on the d: drive or something different. Is there any way of opening this file without specifying the drive? I tried typing: "..\\my_program\\data\\my_file.txt" as well as "../my_program/data/my_file.txt" but nothing has worked yet!! Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)

      P Offline
      P Offline
      Paolo Messina
      wrote on last edited by
      #2

      You may retrieve the current dir, or better the executable path, and then append the name of the file. If you want only the current drive GetCurrentDirectory() is right, just take the first char. If you want the current executable path, use GetModuleFileName(NULL,...) and cut off the text after the last backslash. Then you may append your file's relative path. Paolo.

      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