Thread: read from alternate file on PC

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    read from alternate file on PC

    Hello, I know how to read from a text file that i have saved in the directory where i am compiling from but how do i read or write from a file that is in another directory on my PC?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    use "C:\\<path goes here>" before the path name. So for example:

    your program is running in
    C:\MyProgram\
    Normally when you'd do file.open("file.txt"); it would open it like so: C:\MyProgram\file.txt

    However, when you attach a drive to the beginning, like so: file.open("C:\\file.txt"); it will actually search for the file AT C:\file.txt

    There are also ways to look in the "parent" folder by using .\ or ..\ before the filename. This is useful if you have hierarchy system of files that you want to scan through.

    Also, if you dont' already know, look in to the GetCurrentDirectory and SetCurrentDirectory functions found in windows.h, they are very useful when it comes to accessing files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM