Zweily's small piece (1) document reading and writing in MFC

zhaozj2021-02-16  53

"I finally got it", I only heard that weily belg later in front of the workbench.

"I have been busy for a few days, I finally took the main BUG, ​​I took a break." So Weiily took the table with the cup of Yang Wi-Yee, ready to drink.

"Heily!" Weily was shocked by this sudden sound when he passed the Pisces's seat, and almost threw it out, fooled, just almost ...

"Don't tell you, don't take the speaker so much when you work, you have to know that you almost give my baby cup, but I am not scared, otherwise, this cup you can lose From! "

"... Sorry! Next time, I didn't dare ..." Pisces made a ghost face to give Weily.

"You still want next time ?!"

"No" Pisces rushed to give the speaker, "Yes, weily, can you come over to help me, I am ... um ... a little problem is notified ..."

"Ah? What is the problem?"

"This ... that ..." Pisces seems to be unclear, "I still come over and see it. I will help you to drink."

I haven't waited for the Weily reaction, and the Pisces grabbed the weily of the baby cup and walked into the water driver.

"Be careful! Be careful! That is my cup ..." Weily shook his head, just sat down on the position of Pisces, a bit confused and looked at the screen.

1 minute 18 seconds ......

"Is there a problem?" Pisces suddenly appeared in the back of Weily, with a weily cup, "Well, let's drink something first."

Weily heart is not fixed, the cup in the hand of Pisces, sighed, self-speaking, self-speaking: "No wonder I feel more and more spirituality."

"Well, I will know about it, you come over ..." Weily finished, and drunk a cup.

"........................" I saw that weily sprayed out the one-you-doful thing, "You ... you ... what do you give me a bubble?"

"Nestlé 1 2 coffee?" Pisces replied innocently, "What happened?"

"Hey ... It seems that you still don't understand me! I just want to go to the black tea, but I am ready to add some honey. You should know, I don't drink coffee, even if you drink coffee, you only drink it. Don't add anything! How did you give me this kind of coffee, really, steal my cup ... "Weily looked like a bead.

"Well, I am not good, I am used to drinking this kind of 1 2 coffee, I forgot this, I am sorry, I am sorry. I will refract a cup to you." Pisces admitted error.

"For the time, I will not drink it, wait for me to go to the bubble, save you, give me something to make anything chaos," Weily is accustomed to this style of Pisces, "Well, first said first Say what you have encountered. "

"It's roughly like this: I am a small program read data from a character in a file, then put it in a buffer, and finally output into a new file. I just started learning MFC, I use CFile The class read () function reads a character one by one, and then put it in the object of a CString class, and finally use the WritString () function of the CSTDIOFILE class to output the contents of the buffer to the file. "Pisces point at the screen The code, side saying.

"What is the problem?" Weily asked in deliberately.

"Well, I use UltraEdit to see the result file I output, but I always have a tip when I open: 'do you want to control to dos format?'" "" Oh, where is the problem? Did you know? Understand the consideration of the WritString () function of the CSTDiofile class in the MFC. "Weily said on the desktop MSDN, entered 'cstdiofile' in the index, and then marked the following:

"Text mode provides special processing for carriage return-linefeed pairs. When you write a newline character (0x0A) to a text-mode CStdioFile object, the byte pair (0x0D, 0x0A) is sent to the file. When you read, the byte PAIR (0x0a, 0x0d) is translated to a single 0x0a byte. "

"This thing you always know? What do you talk about what is the main talk?"

"Roughly is CSTDIOFILE to convert it into (0x0D, 0x0a) such a character pair when writing a newline (0x0a), then output. And transition characters (0x0d, 0x0a) conversion when reading It is a single wrap (0x0a). Don't you say it? "

"Yes, didn't say it, do you know the difference between 0x0D and 0x0A?"

"I only know that it is a wrap, one is a bus, which one I just ... um ... I don't know"

"What is the '/ n' it written when you usually write the program?"

"..."

Looking at the Pisces, the weily flashed on the face, but that is just a moment, a short moment of extremely short, it is about 0.05 seconds. However, this moment is still seen by Pisces, but now there is a person, it can only bear it.

"We usually use '/ n' is a newline, '/ r' is the return. The difference between the carriage return and the wrap you should know, I don't say it. The corresponding ASCII code is: 0x0a and 0x0d "

"Well, understand. Is there any relationship with my program?" Pisces is a bit of understanding.

"Hey ... What way you use the file?" Weily asked the childhood.

"One character, read in one by one."

"How do you output it?"

"WriteString () function with cstdiofile is written once."

"Yes! The problem is here. You first open your original file with UltraEdit and the file you generated, use the binary method, pay attention to the place where the file is wrapped in the file, what did you see?"

Pisces is going to do it, then it seems to have found that "the original document is in the place of the line is 2 characters (0x0D, 0x0a), and the file I generated becomes (0x0D, 0x0D, 0x0a) three Character. What is going on? "

"I just let you see the paragraph in msdn? You are in touch with you, think about it!" It seems that weily is a little bit of nature, it can't stand it.

"Oh! I know. I read the character one by one, then I will supervise the '/ r' first, then I will write directly to the cstring, and then read '/ n', and write the cstring. However, when the cstdiofile is output, he will convert the '/ n' back to '/ r', '/ n' character, but there is still one '/ r' in front, so there will be a back The car fisher. "Pisces seems to have experienced. "Well, finally is not a piece of wood, then know how to solve it?"

"Well, I will add a judgment when I read it. If it is '/ r', I will say that I will walk, then I will skip this character, read the next '/ n', write into cstring."

"Well, this problem is basically solved. But do you know if this program?" Weily said with that deliberately detained calm tone. "Once you read the file, you will efficiency. Too bad, give the user feel like a crash, if the user's machine is not fast enough, it will really become a program unresponsive! "

"Ah? This is so serious? Where is the problem?" Pisces looked suspicious, and it didn't believe in Weily.

"Oh, you don't believe it? Then you try it yourself!" Weily gave the Pisces' a stick with a very affirmative tone.

"Well, forget it, I don't try it, I believe you, then you tell me what is the problem?" Pisces's tone is a bit trembling, but it is a bit uncomfortable, but it is not possible to grab the handle by others. The copy.

"Well, tell you. You should have heard of the problem of the efficiency of MFC? Your program can prove this. You are handling the full file again to write the file. But cstdiofile The WriteString () function is very efficient. Once the CString as a parameter is relatively large, this problem will be obvious. "

"What should I do?"

"This is not simple, anyway, you add a statement that judges the wrap, then write it to the file after processing a line!"

"Hey ~~~, it turns out! Well, weily, this time, thank you, please take care of you ..."

I haven't waited for Pisces, weily took his baby cup, muttered: "I have to wash the cup, restorate a cup of black tea, take a break ..."

However, when weily didn't go to the place where the cup washed, the ringtone of the off work rang.

"I wanted to be busy, I can take a break before working, the result ..." Weily sighed a breath, "..."

And Pisces got the program and packed up, ready to go home ...

转载请注明原文地址:https://www.9cbs.com/read-25062.html

New Post(0)