当前位置:网站首页>How to read and write a single document based on MFC
How to read and write a single document based on MFC
2022-07-19 03:29:00 【Yisu cloud】
be based on MFC How to read and write a single document
This article “ be based on MFC How to read and write a single document ” Most people don't quite understand the knowledge points of the article , So I made up the following summary for you , Detailed content , The steps are clear , It has certain reference value , I hope you can gain something after reading this article , Let's take a look at this article “ be based on MFC How to read and write a single document ” Article bar .
Writing documents
Expected effect :

When you click OK, pop up “ Whether to replace ” Prompt box , It refers to whether to replace the content in the original text .
1. Add corresponding menus and ID


Add the corresponding menu and its... In the menu bar ID, And in view.cpp Add the corresponding event handling function
2. Improve the corresponding event response function
This is how we write file function code
void Cdraw3View::OnWriteFile(){ // What to write CString str = _T("hello,world"); // Open the file dialog ,TRUE For reading ,FALSE For writing CFileDialog fileDlg(FALSE); // Determine whether the user clicks OK or cancel if (IDOK == fileDlg.DoModal()) { // Get the file name returned by the dialog CString fileName = fileDlg.GetPathName(); // Open file , Open in write mode , Create... If it doesn't exist CFile file(fileName, CFile::modeWrite | CFile::modeCreate); // write file , The first parameter is the write content , The second parameter is the length of the written content file.Write(str, str.GetLength()); // Close file file.Close(); }}3. Conduct commissioning test
This code is written into the open file hello,world, However, after execution, we opened the document and found that it was not completely written , But only a part of it

This is because our compiler uses unicode code , and unicode The encoded English characters occupy two bytes , The length of the characters we pass in the write function can only write half of the characters to the file

4. Make corresponding modifications according to the test results
Change the length by × One sizeof(TCHAR),TCHAR yes MFC A type definition specified , If it is unicode Character set ,TCHAR It's two bytes ; Other modes may be a byte
// write file , The first parameter is the write content , The second parameter is the length of the written content file.Write(str, str.GetLength()*sizeof(TCHAR));
Debug and test again

Reading documents
Expected effect

1. Add an event response function for reading files

2. Improve the code of event response function
void Cdraw3View::OnReadFile(){ CFileDialog fileDlg(TRUE); if (IDOK == fileDlg.DoModal()) { // Get file path CString path = fileDlg.GetPathName(); // Open as read-only CFile file(path, CFile::modeRead); // Set a read buffer TCHAR* pBuf; // Get the length of the file content int cnt = file.GetLength(); // According to sizeof(TCHAR), Calculate how many characters there are int chars = cnt / sizeof(TCHAR); // Allocate a size for the buffer ,+1 The operation is to add a terminator at the end pBuf = new TCHAR[chars + 1]; // The first is where to put the read content , The second is the read length file.Read(pBuf, cnt); // ending + A terminator pBuf[chars] = 0; // Pop up the read content MessageBox(pBuf); // Close file file.Close(); // Free the requested space after closing the document delete[] pBuf; } }3. Debug test

That's about “ be based on MFC How to read and write a single document ” The content of this article , I believe we all have a certain understanding , I hope the content shared by Xiaobian will be helpful to you , If you want to know more about it , Please pay attention to the Yisu cloud industry information channel .
边栏推荐
- 支持工业级瘦设备4G接入,润和软件DAYU120通过OpenHarmony兼容性测评
- [Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th
- RuntimeError_ Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
- 374. Guess the size of numbers (must be able to get started)
- [MCU simulation] (XX) org - set start address
- Game theory of catching lice
- [MCU simulation] (XV) instruction system bit operation instructions - bit operation instructions, bit conditional transfer instructions
- Polynomial interpolation fitting (II)
- Display zabbix6.0 information using grafana8.5.2
- Chengxin University envi_ IDL second week homework: extract aerosol thickness at n points + detailed analysis
猜你喜欢

Bisenetv1 face segmentation

Powertor500t reports an error 0x01806803

05 central processing unit

Solve the error of 0x00000709 when win10 connects to the shared printer

Using gatekeeper to restrict kubernetes to create specific types of resources

zsh: command not found: mysql

374. 猜数字大小(入门 必会)
![Theoretical basis of double Q-learning and its code implementation [pendulum-v0]](/img/f4/d281bf9e0534aed1d802d32ac35782.png)
Theoretical basis of double Q-learning and its code implementation [pendulum-v0]

Envi: (the most detailed tutorial in 2022) custom coordinate system
![2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )](/img/e4/ff7f1e19583f42377307de7291f870.png)
2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )
随机推荐
Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
[NoSQL] redis high availability and persistence
Leetcode: multiple knapsack problem in dynamic programming [one template solves all ~]
D. Permutation Restoration(贪心/双指针/set)
Cmake common commands
Leetcode: subsequence problem in dynamic programming
論文閱讀:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
论文阅读:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
MySQL replication table
The WinRAR command copies the specified folder as a compressed file, and calls the scheduled task for backup.
Bisenetv2 face segmentation
oracle 查询非自增长分区的最大分区
Wechat applet -- Summary of problems in the actual development of taro framework
Backup kubernetes backup etcd data
Authentication code for wireless
Polynomial interpolation fitting (I)
SQL classic exercises (x30)
Ncnn allocator memory allocator
This is a mathematical problem
Specifications, multi table query basis