CStdioFile readstring () and writestring () precautions

Source: Internet
Author: User

Brief introduction:

CStdioFile

CObject
└cfile
└cstdiofile

text provides special handling of hard carriage return-line break pairs :

When you write a newline character (0x0A) to a text-like CStdioFile object, the byte pair (0x0a,0x0d) is sent to the file, which automatically expands "\ n" to "\ r \ n", which is considered by the individual to be compatible with Windows, but has advantages and disadvantages;

When you read a file, the byte pair (0x0a,0x0d) is translated into a byte (0x0A).
CStdioFile Class Members

Data members

M_pstream Contains a pointer to an open file

Structure
CStdioFile Constructs a CStdioFile object from a path or file pointer

Text Read/write
ReadString Reading a line of text
WriteString Write a line of text


function Prototypes:

Virtual LPTSTR ReadString (LPTSTR lpsz, UINT nMax);
BOOL ReadString(cstring& rString);

virtual void writestring(LPCTSTR lpsz);

Precautions :

Read write files, sometimes for the sake of convenience, like to use ReadString () and WriteString (), because after all, this can be read and write, do not care about the row to differentiate.

But ReadString () and WriteString () to "\ n" Compatibility has broken my big event, for example:

Open file
cstdiofile F;
CFileException e;
if (!f.open ("D:\\test.txt", Cfile::modecreate | Cfile::modenotruncate | Cfile::modereadwrite | Cfile::sharedenynone, &e)) ""
{
	return FALSE;
}

F.writestring ("E:\jiangqin\GameLinker\GameLinker\ version fallback \fiddler\fiddler.exe\r\n");
F.writestring ("E:\jiangqin\GameLinker\GameLinker\ version fallback \fiddler\execaction.exe\r\n");
F.writestring ("E:\jiangqin\GameLinker\GameLinker\ version fallback \fiddler\fiddler.exe.config\r\n");
F.writestring ("E:\jiangqin\GameLinker\GameLinker\ version fallback \fiddler\fiddlerhook\chrome.manifest\r\n");
F.writestring ("E:\jiangqin\GameLinker\GameLinker\ version fallback \fiddler\fiddlerhook\content\about.xul\r\n");
F.close ();

Read File
f.open (M_supgradefile, cfile::moderead);
CString sFile = "";
Vector<cstring> Vecfile;
while (M_hupgradefile.readstring (sFile))
{
	vecfile.push_back (sFile);
}
 
The results are as follows:

E:\jiangqin\GameLinker\GameLinker\ version fallback \fiddler\fiddler.exe\r
E:\jiangqin\GameLinker\GameLinker\ version fallback \ fiddler\execaction.exe\r
E:\jiangqin\GameLinker\GameLinker\ version fallback \fiddler\fiddler.exe.config\r
e:\ Jiangqin\gamelinker\gamelinker\ version fallback \fiddler\fiddlerhook\chrome.manifest\r
E:\jiangqin\GameLinker\ Gamelinker\ version Fallback \fiddler\fiddlerhook\content\about.xul\r
The result is a "\ r" at the end of each file name, which is not what I expected. Why is that so? The reasons are as follows:

Because WriteString () writes the file, it automatically expands "\ n" to "\ r \ n" so that the "\ r \ n" We add becomes "\r\r\n", </span></span

Therefore, when writing a file, the end of the string only needs to add "\ n", so that the end of the data read by the row will not contain "\ r".

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.