MFC program to file processing method _c language

Source: Internet
Author: User

The processing of files is a very common application in MFC programming. In this paper, a simple description of the form of a case. The specific methods are as follows:

Application of 1.CFileDialog

The format is as follows:

Cfiledialog::cfiledialog (BOOL bopenfiledialog, LPCTSTR lpszdefext = null, LPCTSTR lpszFileName = null, DWORD dwFlags = of n_hidereadonly | Ofn_overwriteprompt, LPCTSTR lpszfilter = null, cwnd* pParentWnd = null); 

The specific parameters are explained as follows:

Bopenfiledialog is true to display the Open dialog box, or False to display the Save Dialog File dialog box .
Lpszdefext Specifies the default file name extension .
lpszFileName Specifies the default filename .
dwflags indicates some specific style .
Lpszfilter is the most important parameter that indicates the type of file to choose from and the appropriate extension . Parameter formats such as:
pParentWnd is the parent window pointer .

The specific code is as follows:

const int int_max_file_num = 1; 
TCHAR szfilefilter[] = _t ("Date File (*.csv) |*.csv|");
TCHAR *pszfilenamebuf = new tchar[int_max_file_num * _max_path];

CFileDialog Dlgopenfile (FALSE, _t ("CSV"), NULL, Ofn_hidereadonly | Ofn_overwriteprompt, Szfilefilter, this);
:: memset (pszfilenamebuf, 0, Int_max_file_num * _max_path * sizeof (TCHAR) );
DlgOpenFile.m_ofn.nMaxFile = Int_max_file_num * _MAX_PATH;    
DlgOpenFile.m_ofn.lpstrFile = Pszfilenamebuf;    
Dlgopenfile.m_ofn.lpstrfile[0] = NULL;
if (Dlgopenfile.domodal = = Idok)
{
//get filename
}

Here's what you need to be aware of:

(1) Dlgopenfile when the first argument is false, the Save dialog box is displayed, and when True, the Open dialog box is displayed

(2) File type description and extension between | Delimited, can be used between extensions of the same type of file; Split, between each file type | To divide or end with; specified, otherwise it will cause garbled

Get the saved file name:

CString cfiledialog::getpathname () Gets the full file name, including the directory name and extension

CString cfiledialog::getfilename () get the full filename

CString cfiledialog::getextname () get the full file name extension

CString cfiledialog::getfiletitle () Gets the full file name, including the directory name and extension

POSITION cfiledialog::getstartposition () get the first file location for multiple files selected

2. Writing to a. csv file

Due to. The content written in the CSV file is presented as a table, so it is more commonly used when viewing content as a file in most structures saved to a file.

Here's what you need to be aware of:

(1) If it is written like a normal file, it is obvious that the file is stored in double bytes, which results in a view of the problem, so I use the Cstdiofilel class in this place, and finally I can write the characters to the file using WriteString ()

(2) When the characters appear in Chinese, resulting in Chinese after all can not be written to the file, then need to write the file writestring () before adding a sentence setlocale (lc_ctype, "CHS");

(3) When the character is completed in a table, you need to add ', ' after the string, you need to change the line, you need to insert ' \ r \ n ' after the string;

For example:

Strtemp.format (_t ("%s,"), Lvcol.psztext);
Strtemp.format (_t ("%s\r\n"), Lvcol.psztext);

(4) In general, the use of CString will be written to the contents of the file after all save, write the file together;

The specific code is as follows:

CString Strex; 
Strex + = strtemp;

I hope the example described in this article will help you with the MFC program design.

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.