Return to the C++ Demos Home Page
SAMS Teach Yourself C++ in 21 Days
Chapter Sixteen
Prog 1 : Using cin.get()
// STYC++ in 21 chapter 16
//
// Simple I/O operations - using cin.get()
//
//
#include <iostream.h>
int main()
{
char stringOne[256];
char stringTwo[256];
cout << "Enter String One : ";
cin.get(stringOne,256); // works for the line
cout << "stringOne is : " << stringOne << endl;
cout << "Enter String Two : ";
cin >> stringTwo; // takes up to the first whitespace
cout << "stringTwo is : " << stringTwo << endl;
return 0;
} // eos
Prog 2 : Using cin.ignore()
// STYC++ in 21 chapter 16
//
// Simple I/O operations - using cin.get() with cin.ignore
//
//
#include <iostream.h>
int main()
{
char stringOne[256];
char stringTwo[256];
cout << "Enter String One : ";
cin.get(stringOne,256); // works for the line
cout << "stringOne is : " << stringOne << endl;
cout << "Enter String Two : ";
cin.getline(stringTwo,256); // trouble with rest of input buffer
cout << "stringTwo is : " << stringTwo << endl;
cout << "\n\n Let's try this again!!\n";
cout << "Enter String One : ";
cin.get(stringOne,256); // works for the line
cout << "stringOne is : " << stringOne << endl;
cin.ignore(255,'\n'); // clears out to the end
cout << "Enter String Two : ";
cin.getline(stringTwo,256); // now we're good
cout << "stringTwo is : " << stringTwo << endl;
return 0;
} // eos
Prog 3 : Using peek() & putback()
// STYC++ in 21 chapter 16
//
// Peek and putback funcs in cin
//
//
//
#include <iostream.h>
int main()
{
char ch;
cout << "Enter a phrase: ";
while ( cin.get(ch) )
{
if ( ch == '!' ) cin.putback('$');
else cout << ch;
while (cin.peek() == '#' ) cin.ignore(1,'#');
}
return 0;
} // eos
Prog 4: Using cout.write()
// STYC++ in 21 chapter 16 prog 4
//
// using cout.write
//
//
#include <iostream.h>
#include <string.h>
int main()
{
char one[] = "One if by land";
int fullLength = strlen(one);
int tooShort = fullLength - 4;
int tooLong = fullLength + 6;
cout.write(one, fullLength) << '\n';
cout.write(one, tooShort) << '\n';
cout.write(one, tooLong) << '\n';
return 0;
} //eos
Prog 5 : Using cout.width()
// STYC++ in 21 days chapter 16 prog 5
//
// Manipulating width with cout.width()
//
//
#include <iostream.h>
int main()
{
cout << "Start >";
cout.width(25);
cout << 123 << "< End\n";
cout << "Start >";
cout.width(25);
cout << 123 << "< Next >";
cout << 456 << "< End\n";
cout << "Start >";
cout.width(4);
cout << 123456 << "< End\n";
return 0;
} // eos
Prog 6 : Using good old printf() and the stdio
// STYC++ in 21 days chapter 16 prog 6
//
// Old School!! Because everyone and their mother
// loved the ease of printf the book points out that
// most implementations of C++ include (no pun intended)
// all of the old C libraries. Hello stdio!! Now if we
// can convince them to use scanf() again we'll be OK
//
// with the exception of my slashes for comments this
// is basically a C program. The first four lines
// constitute the first C program everyone ever wrote.
//
/* Just to see */
#include <stdio.h> // feels good just to type it
int main()
{
printf("%s", "hello world!\n");
char* phrase = "Hello again!\n";
printf("%s", phrase);
int x = 5;
printf("%d\n", x);
char* phrase1 = "Here's some values: ";
char* phrase2 = " and also these : ";
int y=7, z=35;
long longVar = 36465;
float floatVar = 8.8f;
printf("%s %d %d %s %ld %f\n",phrase1,y,z,phrase2,longVar,floatVar);
char* phrase3 = "Formatted: ";
printf("%s %5d %10d %10.5f\n", phrase3,y,z,floatVar);
return 0; // didn't need this before the ANSI made main an int...
} // eos
Prog 7 : Basic File I/O
// STYC++ in 21 days chapter 16 prog 7
//
// Basic file I/O
//
//
#include <fstream.h> // includes iostream
int main()
{
char filename[80];
char buffer[255]; // for user input
cout << "Enter filename : ";
cin >> filename;
ofstream fout(filename); // open the file for writing
fout << "This line is written directly to the file\n";
cout << "Enter the text for the file : ";
cin.ignore(1,'\n'); // eat the newline after the filename
cin.getline(buffer,255); // get the user input
fout << buffer << '\n'; // then write it to the file
fout.close(); // close out the write, get ready to open for input
ifstream fin(filename); // open for reading
cout << "Here's the contents of the file : \n";
char ch;
while (fin.get(ch)) cout << ch;
cout << "\n\n **** end of file **** \n";
fin.close(); // always clean your room
return 0;
} // eos
Prog 8 : Writing a class object to file and reading it back
// STYC++ in 21 days chapter 16 prog 8
//
// Writing and reading a class object to and from a file
// Writes contents of first object into the second with file I/O
//
#include <fstream.h>
class Animal
{
public:
Animal(int weight,long days):itsWeight(weight),DaysAlive(days){}
~Animal(){}
int GetWeight()const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
long GetDaysAlive()const { return DaysAlive; }
void SetDaysAlive(long days) { DaysAlive = days; }
private:
int itsWeight;
long DaysAlive;
};
int main() // returns 1 on error
{
char fileName[80];
cout << "Please enter the file name: ";
cin >> fileName;
ofstream fout(fileName,ios::binary);
if (!fout)
{
cout << "Unable to open " << fileName << " for writing.\n";
return(1);
}
Animal Bear(50,100);
fout.write((char*) &Bear,sizeof Bear);
fout.close();
ifstream fin(fileName,ios::binary);
if (!fin)
{
cout << "Unable to open " << fileName << " for reading.\n";
return(1);
}
Animal BearTwo(1,1);
cout << "BearTwo weight: " << BearTwo.GetWeight() << endl;
cout << "BearTwo days: " << BearTwo.GetDaysAlive() << endl;
fin.read((char*) &BearTwo, sizeof BearTwo);
cout << "BearTwo weight: " << BearTwo.GetWeight() << endl;
cout << "BearTwo days: " << BearTwo.GetDaysAlive() << endl;
fin.close();
return 0;
}
Prog 9 : Command line argument processing
// STYC++ in 21 days chapter 16 prog 9
//
// Command line parameters.....
//
// Remember that argv[0] is the name of the prog
//
#include <iostream.h>
int main(int argc, char **argv)
{
cout << "Received " << argc << " Arguments...\n";
for ( int i = 1; i < argc; i++ ) cout << "Arg " << i << " : " << argv[i] << endl;
return 0;
} //eos