發新話題
打印

Stream Input/Output

Stream Input/Output


  • #include <iostream>
  • using std::cout;
  • using std::endl;

  • int main()
  • {
  •         char *word = "again";
  •         //display value of char*, the display value of char*
  •         //static cast to void*
  •         cout << "Value of word is: " << word << endl << "Vaule of static_cast< void * (word) is:" << static_cast< void * >(word) << endl;
  •                 return 0;
  • }//end main

TOP

Steam Output


  • #include <iostream>
  • using std::cout;
  • using std::endl;

  • int main()
  • {
  •         char *word = "again";
  •         //display value of char*, the display value of char*
  •         //static cast to void*
  •         cout << "Value of word is: " << word << endl << "Vaule of static_cast< void * (word) is:" << static_cast< void * >(word) << endl;
  •                 return 0;
  • }//end main

TOP

Using member functions get, put and eof

  • // Using member functions get, put and eof.
  • #include <iostream>
  • using std::cin;
  • using std::cout;
  • using std::endl;
  • int main()
  • {
  •   int character; // use int, because char cannot represent EOF
  •   // prompt user to enter line of text
  •   cout << "Before input, cin.eof() is:" << cin.eof() << endl << "Enter a sentence followed by end-of-line:" << endl;
  •   //use get to read each character; use put to display it
  •   while (( character = cin.get() ) != EOF )
  •   cout.put (character);
  •   // display end of  file character
  •   cout << "\nEOF in this system is : " << character << endl;
  •   cout << "After input of EOF, cin.eof() is " << cin.eof() << endl;
  •   return 0;
  •   } //end main

TOP

Inputting characters using cin member function getline


  • //Inputting characters using cin member function getline

  • #include <iostream>
  • using std::cin;
  • using std::cout;
  • using std::endl;

  • int main()
  • {
  • const int SIZE = 80;
  • char buffer [SIZE]; // create array of 80 characters

  • // input characters in buffer via cin function getline
  • cout << "Enter a sentence:" << endl;
  • cin.getline( bufer, SIZE );

  • //display buffer contents
  • cout << "\nThe sentence entered is:" << endl << buffer << endl;
  • return 0;
  • }//end main

TOP

Unformat IO using gcount, read, write

  • #include <iostream>
  • using std::cin;
  • using std::cout;
  • using std::endl;
  • int main()
  • {
  • const int SIZE = 80;
  • char buffer[SIZE]; //creat array of 80 characters
  • // use function read to input characters into buffer
  • cout << "Enter a sentence:" << endl;
  • cin.read( buffer, 20);
  • //use functions write and gcont to display buffer characters
  • cout << endl << "The sentence entered was:" << endl;
  • cout.write( buffer, cin.gcount() );
  • cout << endl;
  • return 0;
  • }// end main


[ 本帖最後由 Net 於 2010-7-26 13:27 編輯 ]

TOP

發新話題