C ++ study notes, file reading, string-related

Source: Internet
Author: User

1. String comparison function int strcmp (char * str1, char * str2) returns 0 when the two strings are the same (while the strcmp of matlab returns 1 ). View Asic code, str1> str2, return value> 0; str1 <0.
2. Use a string stream to convert a string to a number, as shown in the following code segment:
A) numeric to string:
# Include
# Include
String num2str (double I)
{
Stringstream ss;
Ss <I;
Return ss. str ();
}
B) string to numeric:
Int str2num (string s)
{
Int num;
Stringstream ss (s );
Ss> num;
Return num;
}
3. Calculate the data size of the binary file
A) Functions for reading and writing binary files: ifstream if. read (), ofstream of. write ();
B) file stream pointer locating function: if. tellg (), of. tellp ();
C) file stream pointer change function: if. seekg (), of. seekp ();
Function parameters:
Ios: beg displacement calculated from the starting position of the stream ios: cur displacement calculated from the current position of the stream pointer ios: end displacement calculated from the end of the stream
D) Get the file size code:
// Obtaining file size # include const char * filename = "example.txt"; int main () {long l, m; ifstream file (filename, ios: in | ios:: binary); l = file. tellg (); file. seekg (0, ios: end); m = file. tellg (); file. close (); cout <"size of" <filename; cout <"is" <(m-l) <"bytes. \ n "; return 0 ;}
4. String stream member function str
A) stringstream ss;
String temp_str = ss. str (); obtain the string in the string stream buffer.
B) stringstream ss;
String temp_str = "aaa ";
Ss. str (temp_str); assign a value to the string stream.
C) to reuse a stream, clear the empty stream first and use ss. clear ().

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.