C ++ _ note_10_io

Disclaimer: This article is a blogger original article, please follow bloggers Zhiyi Sheng forwarding. https://blog.csdn.net/Ga4ra/article/details/89853026

11. I

c ++ io of three parts: a keyboard, a file, memory, are referred to a standard i / o, the file i / o, serial i / o.

ios
istream
ostream
ifstream
istringstream
iostream
ofstream
ostringstream
fstream

11.1 Standard io

11.1.1 standard input stream

#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	int a;
	float b;
	char s[10];
	cin>>a;
	cin>>b;
	cin>>s; //不能接收空格
	
	cout<<a<<endl;
	cout<<b<<endl;
	cout<<s<<endl;
	cin.get();
	return 0;
}

/*
1
1.2
a b
1
1.2
a
*/

Exists between the input buffer and the Keyboard, exists between the output buffer and a screen.

When the input buffer if no data will be blocked.

#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	char c;
	while( (c = cin.get()) != EOF)
	{
		cout<<c<<endl;
	}
	cin.get();
	return 0;
}

/*
aa	//此时已经输入input buf
a
a


^Z
请按任意键继续. . .
*/

cin.get ():

  • istream& get (char& c);
  • istream& get (char* s, streamsize n);
  • istream& get (char* s, streamsize n, char delim);
  • istream& get (streambuf& sb);
  • istream& get (streambuf& sb, char delim);
#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	char a,b,c;
	cin.get(a);
	cin.get(b);
	cin.get(c);
	
	cout<<a<<b<<c<<"there is data in buf.\n";
	
	cin.get(a).get(b).get(c);
	cout<<a<<b<<c<<endl;
	
	cin.get();
	return 0;
}
/*
abcdefg
abcthere is data in buf.
def
*/

The input string containing spaces by getline():

  • istream& getline (char* s, streamsize n );
  • istream& getline (char* s, streamsize n, char delim );
#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
	char s[10];
	cin.getline(s,10);
	cout<<s<<endl;
	
	cin.get();
	return 0;
}

/*
aaaaa sssss dd
aaaaa sss
*/

The following are three non-common functions:

  • istream& ignore (streamsize n = 1, int delim = EOF);,ignore until delim
  • int peek(); Detect whether there is a data buffer
  • istream& putback (char c); The return buffer c
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
	char buf1[10],buf2[10];
	int ret;
	cin>>buf1;
	cin.ignore(1);
	ret = cin.peek();
	cout<<"ret:"<<ret<<endl;
	
	cin>>buf2;
	cout<<"buf1:"<<buf1<<endl;
	cout<<"buf2:"<<buf2<<endl;
	
	cin.get();
	return 0;
}

/*
aa  bb
ret:32
buf1:aa
buf2:bb
*/

The following is the official website given putback()example.

// istream::putback example
#include <iostream>     // std::cin, std::cout
#include <string>       // std::string

int main () {
  std::cout << "Please, enter a number or a word: ";
  char c = std::cin.get();

  if ( (c >= '0') && (c <= '9') )
  {
    int n;
    std::cin.putback (c);
    std::cin >> n;
    std::cout << "You entered a number: " << n << '\n';
  }
  else
  {
    std::string str;
    std::cin.putback (c);
    getline (std::cin,str);
    std::cout << "You entered a word: " << str << '\n';
  }
  return 0;
}

11.1.2 Standard Output

Definition standard output stream:extern ostream cout;

Common functions:

  • ostream& flush(); So that information immediately forced output buffer.
  • ostream& put (char c);cout.put('a').put('b');
  • ostream& write (const char* s, streamsize n);, The output binary stream.
  • streamsize width();
  • char fill();
  • fmtflags setf();
char *p = "hello";
cout.write(p,5)<<endl;
#include<iostream>
using namespace std;
#include <iomanip>
int main(int argc, char *argv[])
{
   cout<<"<start>"
   	<<setw(30)
   	<<setfill('*')
   	<<setiosflags(ios::showbase)
   	<<hex
   	<<100
   	<<"<end>"
   	<<endl;
   cout<<endl;
   cout.width(5);
   cout.fill('*');
   cout.setf(ios::showbase);
   cout.setf(ios::internal);
   cout<<hex<<100<<endl;
   
   cin.get();
   return 0;
}

/*
<start>**************************0x64<end>

0x*64
*/

11.2 file io

open a file:void open (const char* filename, ios_base::openmode mode = ios_base::out);

Call to bool is_open() const;determine whether the open was successful.

Read and write functions:

  • ostream& write (const char* s, streamsize n);
  • istream& read (char* s, streamsize n);

Entry

std::ifstream ifs;
ifs.open ("test.txt", std::ifstream::in);

char c = ifs.get();
while (ifs.good()) {
    std::cout << c;
    c = ifs.get();
}

ifs.close();
member constant stands for access
in * input File open for reading: the internal stream buffer supports input operations.
out output File open for writing: the internal stream buffer supports output operations.
binary binary Operations are performed in binary mode rather than text.
ate at end The output position starts at the end of the file.
app append All output operations happen at the end of the file, appending to its existing contents.
trunc truncate Any contents that existed in the file before it is open are discarded.

Export

std::ofstream ofs;
ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);
ofs << " more lorem ipsum";
ofs.close();
//or
//std::ofstream fout("test.txt");

Notice that outway, if the file exists, then clear the contents.

member constant stands for access
in input File open for reading: the internal stream buffer supports input operations.
out * output File open for writing: the internal stream buffer supports output operations.
binary binary Operations are performed in binary mode rather than text.
ate at end At The the Output position Soho starts at The AT at The End of File.
File does not exist error
app append All output operations happen at the end of the file, appending to its existing contents.
trunc truncate Any contents that existed in the file before it is open are discarded.

Guess you like

Origin blog.csdn.net/Ga4ra/article/details/89853026