What is the difference between opening a file in IOS: Binary and IOs: Text? fstream reads/writes a file.

Source: Internet
Author: User

Summarize the online search content:

1. When a file is written in text mode, the newline (/N) is extended to/R/n. When the file is read, it is automatically converted back.

2. the binary method does not perform any expansion, which is consistent with the read/write content.

3. The default value is text.

 

Http://blog.csdn.net/wanfustudio/archive/2007/09/14/1785131.aspx

 

Appendix: two self-written functions for reading and writing files using fstream

# Include <fstream>

Using namespace STD;

 

Bool preprocess: readparsersltfile (tstring filename)

{

Tchar szfull [max_path] = {0 };

_ Tcscpy_s (szfull, max_path, m_szdatafiledir );

_ Tcscat (szfull, text ("//"));

_ Tcscat (szfull, filename. c_str ());

_ Tcscat (szfull, text ("_ r "));

 

Wifstream fin;

Fin. Open (szfull, IOS: Binary );

If (Fin. Bad ())

{

Return false;

}

Fin. imbue (STD: locale ("CHS "));

 

Tchar szsize [16] = {0 };

Fin. Getline (szsize, 16); // the size of the first behavior File

Int nsize = _ ttoi (szsize );

 

Tstring strtmp;

For (INT I = 0; I <nsize; I ++)

{

Getline (FIN, strtmp );

M_vsenparrslt.push_back (strtmp );

}

Fin. Close ();

 

Return true;

}

Void preprocess: writefiles (tstring filename, const vector <tstring> & vcontent)

{

Tchar szfull [max_path] = {0 };

_ Tcscpy_s (szfull, max_path, m_szdatafiledir );

_ Tcscat (szfull, text ("//"));

_ Tcscat (szfull, filename. c_str ());

 

Wofstream fout;

Fout. Open (szfull, IOS: Binary | ios_base: trunc );

// Fout. Clear ();

Fout. imbue (STD: locale ("CHS "));

Fout <vcontent. Size () <"/N ";

For (INT I = 0; I <vcontent. Size (); I ++)

{

Fout <vcontent [I]. c_str () <"/N ";

}

Fout. Close ();

}

The written content contains Chinese characters, so you must add: fout. imbue (STD: locale ("CHS "));

Tstring is a custom macro, representing string or wstring

# Ifdef _ Unicode

# Define tstring wstring

# Else

# Define tstring string

# Endif

Related Article

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.