Solved

How do I use CFIle with path lengths greater than MAX_PATH?

Posted on 2004-04-01
4
896 Views
Last Modified: 2013-11-20
I need to be able to save files into an absolute location whose full path is more than MAX_PATH characters.  My application is not (and can not easily be) in Unicode.  Could I use a relative path name of fewer than MAX_PATH characters and if so, what is the best way to set the desired current directory.  My appication is currently using CFile for file handling.  Please provide a few lines of code by way of an example if not very straightforward.
0
Comment
Question by:PhilipG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Assisted Solution

by:lakshman_ce
lakshman_ce earned 100 total points
ID: 10734754
Plz have a look at this for obtaining relative file names,
http://www.codeguru.com/Cpp/misc/misc/fileanddirectorynaming/print.php/c263/

-Lakshman
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10735729
You could try _chdir to set the current directory in stages
eg.
_chdir("C:\\temp");
_chdir(".\\subDir1");
_chdir(".\\SubDir2");
and so on where you split your path (recursive function) and move down the directory chain for each directory.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 150 total points
ID: 10736448
You'll still run into problems because CFile::Open() calls a function named AfxFullPath() that expands the file name to the full path with the limitation of _MAX_PATH. Look at FILECORE.CPP in the MFC\SRC directory.

What you might try is to derive a new class from CFile and override the Open method fixing the _MAX_PATH limitations. You'll want to look at the FILECORE.CPP to see what else you'd have to override/fix.

Then you could derive from your new class and attempt to open the long file name.

Good Luck,
Steve
0
 

Author Comment

by:PhilipG
ID: 10737161
Thanks all for the very quick response.  I have awarded the points although in the end I adopted a different solution.  The reason for the high point was that I wanted ideas very quickly - it worked.

The solution I used was to use CreateFileW and to convert my path to Unicode using the A2W macro.  I was also able to set the attributes by using SetFileAttributesW and the Unicode path.  The Microsoft info is:

HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

Parameters
lpFileName
[in] Pointer to a null-terminated string that specifies the name of the object to create or open.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

Thanks for the quick response.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question