The interesting phenomenon of opening files in the cfile class

Source: Internet
Author: User

The interesting phenomenon of opening files in the cfile class

By lypindream

Download source code

Apply in preparation dialog boxProgramIn the process, I found a very interesting phenomenon, that is, when you use the open member function of the cfile class to open a file, if the file path is not specified, it will be the path of the current program by default, however, if you use the cfiledialog class in the open file dialog box and open a file, the default path of the open member function file will be the path of the cfiledialog class to open the file. The following details:

Cfile: Open
Virtual bool open (maid, uint nopenflags, cfileexception * perror = NULL );

Return Value: If it is enabled successfully, a non-0 value is returned; otherwise, 0 is returned. The perror parameter is valid only when 0 is returned.
Parameter: lpszfilename: a string that specifies the path of the required file.
Nopenflag is a uint value that defines the file sharing and access methods.
Perror identifies a pointer to an existing file exception object, which indicates the completion status of the open operation.

Note: Open functions are designed to work with the default cfile constructor.
I use the followingCodeTo obtain the default path of the currently opened file:

Cstring strcurrentpath, strmsg; getcurrentdirectory (200, strcurrentpath. getbuffer (200); strcurrentpath. releasebuffer (); strmsg = _ T ("current file opening path: \ n") + strcurrentpath; MessageBox (strmsg );

The Path obtained when I did not use the File Open dialog box is as follows:

The Path obtained when I opened c: \ Program Files \ desktop. ini in the open file dialog box is as follows:

If you want to use cfile: open to open the file in the path of the program after using the file open dialog box cfiledialog during programming, you can only use the Windows API function getcurrentdirectory () to obtain the path and save it with variables before using the file open dialog box cfiledialog.

The above is a little bit of experience in the programming process of the author. I am very smiling. Please give me more advice.

Not only the cfiledialog class,

If onopendocument is reloaded and cfile is used to open the file,

This problem also occurs. (Jaketseng was published on 12:09:00)
 
Thanks to both the landlord and chpsoft! (Indicated by lingju at 17:23:00)
 
Great help!
I'm dizzy with this strange problem! (Indicated by lingju at 17:23:00)
 
Naive! (Joybaby posted on 23:42:00)
 
It makes sense. It's just chpsoft's suggestion. (Zx9506 was published at 11:43:00)
 
The above brother, you have made a mistake. After the cfiledialog class is used, the path of the current directory is changed by default. Therefore, if you want to avoid this problem, add the ofn_nochangedir flag in use (chpsoft is published at 12:54:00)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.