Is there any difference between ios_base::out and ios::out, ios_base::in and ios::in, ios_base::app and ios::app, etc.?

Wednesday evening, August 2, 2023

Today I saw these two lines of code:

std::ofstream file("example.txt", std::ios_base::out);

std::ofstream file("example.txt", std::ios::out);

This raised a few questions for me:

Why do I sometimes use ios_base::out and sometimes ios::out?

Is there a difference between ios_base::out and ios::out?

It took me an hour or two to figure it out....


Table of contents


in conclusion

There is no difference between ios_base::out and ios::out, they are exactly the same.

Because they are all integer constants and have the same values, they can completely replace each other.

As for when to use which one, I think use whichever one you like because they are really the same.

What are the I/O flow control flags of the ios_base class?

  • std::ios_base::in: Input mode, indicating that the stream is opened in reading mode.
  • std::ios_base::out: Output mode.
  • std::ios_base::app: Append mode, which means opening the stream in writing mode and appending data to the end of the file without clearing the original content.
  • std::ios_base::trunc: Truncate mode, which means opening the stream in writing mode and clearing the original content of the file.
  • std::ios_base::binary: Binary mode, open the stream in binary mode.
  • std::ios_base::ate: Position the file pointer to the end of the file after opening the stream.

What are the I/O flow control flags of the iOS class?

  • std::ios::in: Input mode, indicating that the stream is opened in reading mode.
  • std::ios::out: Output mode.
  • std::ios::app: Append mode, which means opening the stream in writing mode and appending data to the end of the file without clearing the original content.
  • std::ios::trunc: Truncate mode, which means opening the stream in writing mode and clearing the original content of the file.
  • std::ios::binary: Binary mode, open the stream in binary mode.
  • std::ios::ate: Position the file pointer to the end of the file after opening the stream.

prove that they are the same

#include<iostream>
#include<fstream>


int main(){
	printf("ios_base::in的值:%d\n",std::ios_base::in);
	printf("ios::in的值:%d\n",std::ios::in);
	printf("-------------\n");
	
	printf("ios_base::out的值:%d\n",std::ios_base::out);
	printf("ios::out的值:%d\n",std::ios::out);
	printf("-------------\n");
	
	printf("ios_base::app的值:%d\n",std::ios_base::app);
	printf("ios::app的值:%d\n",std::ios::app);
	printf("-------------\n");
	
	printf("ios_base::trunc的值:%d\n",std::ios_base::trunc);
	printf("ios::trunc的值:%d\n",std::ios::trunc);
	printf("-------------\n");
	
	printf("ios_base::binary的值:%d\n",std::ios_base::binary);
	printf("ios::binary的值:%d\n",std::ios::binary);
	printf("-------------\n");
	
	printf("ios_base::ate的值:%d\n",std::ios_base::ate);
	printf("ios::ate的值:%d\n",std::ios::ate);
	printf("-------------\n");
	
	
}

 

Guess you like

Origin blog.csdn.net/m0_61629312/article/details/132073083