现在的位置: 首页 > 综合 > 正文

c++IO 操作

2018年02月16日 ⁄ 综合 ⁄ 共 6035字 ⁄ 字号 评论关闭
文章目录

 The IO types are defined in three separate headers: iostream defines
the types used to read and write to a console window, fstream defines the types used to
read and write named files, and sstream defines the types used to read
and write in-memory strings.

istringstream

 

// 输入一个字符

// istream& get ( char& c );  Extracts a character from the stream and stores it in c.

#include <iostream>
using namespace std;

int main ()
{
    char a;
    cin.get(a);
    cout << a << endl;

  return 0;
}

///////////////////////////////////////////////

// 得到指定长度的c风格的字符串

// istream& get (char* s, streamsize n );
// Extracts characters from the stream and stores them as a c-string into the array beginning at s.
// Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '/n' is found.

#include <iostream>
using namespace std;

int main ()
{

    char str[8];
    cin.get( str, 8);
    cout << str  << endl;

  return 0;
}

result:

abcdefghijk
abcdefg

///////////////////////////////////////////////////////////////////////////////////

// 指定分割字符

#include <iostream>
using namespace std;

int main ()
{
//istream& get (char* s, streamsize n, char delim );
    //Same as above, except that the delimiting character is the one specified indelim instead of '/n'..
    char str[8];
    cin.get( str, 8,'c');
    cout << str  << endl;

  return 0;
}
result:

abcdefghijk
ab

/////////////////////////////////////////////////////////////////////////////////////

//清除缓冲区

// 加同步

#include <iostream>
using namespace std;
int main ()
{
    char a;
    cin.get(a);
    cin.clear();//清除缓冲区
    cin.sync(); // Synchronize input buffer with source of characters ,不然,下句无效
    cout << a << endl;
    char str[8];
    cin.get(str, 8);
    cout << str << endl;
    return 0;
}

//////////////////////////////////

// 从文件中得到到个字符

#include <fstream>
#include <iostream>
using namespace std;

void main()
{
    char c ,str[256];
    ifstream is;
    cout << "enter the name of an existing text file" << endl;
    cin.get(str, 256 );
    is.open( str );
    while( is.good() )
    {
        c = is.get(); // get charactor from file
        if (is.good() )
            cout << c;
    }
    is.close();
}

// ios::good

//Check if the state of the stream is good for i/o operations.

// The function returns true if none of the stream's error flags (eofbit, failbit and badbit) are set.

///////////////////////////////////////////

 badbit   发生致命错误
 eofbit    EOF已经找到 end of file
 failbit    已找到一个非致命性错误
 goodbit 没有错误发生 

 ios::app 追加到输出
 ios::ate 打开文件的时候定位到 EOF
 ios::binary 二进制打开文件
 ios::in 打开文件读
 ios::out 打开文件写
 ios::trunc 覆盖已存在的文件

///////////////////////////////////////////////////////////////

// 按行输入

#include <fstream>
#include <iostream>
using namespace std;

void main()
{
    char name[30];
    cout << "enter your name" << endl;
    cin.getline(name,30); // 输入终止条件:length >=30 || 遇到了回车(默认的分割符)
    cout << name << endl;
}

////////////////////////////////////////////////////////////////////////////////////////////

// ignore 用法

#include <iostream>
using namespace std;

int main ()

{
  char first, last;

  cout << "Enter your first and last names: ";

  first=cin.get();
  cin.ignore(256,' ');

  last=cin.get();

  cout << "Your initials are " << first << last;

  return 0;
}

/////////////////////////////////////////////////////////////

// peek 用法

Reads and returns the next character without extracting it

// istream peek
#include <iostream>
using namespace std;

int main () {
  char c;
  int n;
  char str[256];

  cout << "Enter a number or a word: ";
  c=cin.peek();
  // c = cin.get(); //if you enter the word that vlisa, it output lisa
  if ( (c >= '0') && (c <= '9') )
  {
    cin >> n;
    cout << "You have entered number " << n << endl;
  }
  else
  {
    cin >> str;
    cout << " You have entered word " << str << endl;
  }

  return 0;
}

/////////////////////////////////////////////////////////////

// 以块的方式读取文件中的数据,或输入数据read , write

#include <iostream>
#include <fstream>
using namespace std;
// cin.read and cout.write
int main ()
{
   int length;
   char * buffer;

   ifstream is;
   is.open( "test.txt", ios::binary ); // 以二进制格式打开文件
  
   // 得到文件长度
   is.seekg(0, ios::end); // 偏移量加指定位置
   length = is.tellg();   // 返回当前 得到指针的位置
   is.seekg(0, ios::beg);

   //allocate memory
   buffer = new char[length];
  
   // read data as a block 以块的方式读取文件中的数据
   is.read(buffer, length );

   is.close();

   // output data to screen
   cout.write( buffer, length );
   return 0;
}


/*

istream& seekg ( streampos pos );
istream& seekg ( streamoff off, ios_base::seekdir dir );

 

Set position of the get pointer

Sets the position of the get pointer.
The get pointer determines the next location to be read in the source associated to the stream.

Parameters

pos
The new position in the stream buffer. This parameter is an integral value of type streampos.
off
Integral value of type streamoff representing the offset to be applied relative to an absolute position specified in the dir parameter.
dir
Seeking direction. It is an object of type ios_base::seekdir that specifies an absolute position from where the offset parameter off is applied. It can take any of the following member constant values:
value offset is relative to...
ios_base::beg beginning of the stream buffer
ios_base::cur current position in the stream buffer
ios_base::end end of the stream buffer

 

streampos tellg ( );

 

Get position of the get pointer.

Returns the absolute position of the get pointer.

*/

////////////////////////////////////////

#include <iostream>
using namespace std;

int main () {
  char c;
  int n;
  char str[256];

  cout << "Enter a number or a word: ";
  c = cin.get();

  if ( (c >= '0') && (c <= '9') )
  {
    cin.putback (c);
    cin >> n;
    cout << "You have entered number " << n << endl;
  }
  else
  {
    cin.putback (c);
    cin >> str;
    cout << " You have entered word " << str << endl;
  }

  return 0;
}

////////////////////////////////////////////////

ostream

put  put character

write write black of data

tellp     Get position of put pointer (public member function)
seekp    Set position of put pointer (public member function)

//////////////////////////////////////////////////////////////////////////////

// modify precision
#include <iostream>
using namespace std;

int main ()
{
double f = 3.14159;
cout.setf(0,ios::floatfield); // floatfield not set
cout.precision(5);
cout << f << endl;
cout.precision(10);
cout << f << endl;
cout.setf(ios::fixed,ios::floatfield); // floatfield set to fixed
cout << f << endl;
return 0;
}
/////
3.1416
3.14159
3.1415900000
//////////////////////////////////////////
istringstream
istringstream provides an interface to manipulate strings as input streams.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main () {

int n,val;
string stringvalues;

stringvalues = "125 320 512 750 333";
istringstream iss (stringvalues,istringstream::in);

for (n=0; n<5; n++)
{
iss >> val;
cout << val*2 << endl;
}

return 0;
}
//////////////////////
250
640
1024
1500
666
//////////////////////////////////////////////////////////////////////////////////////////////////////

抱歉!评论已关闭.