File I/O Notes

File I/O


Searching for the end of the file



Getline vs. cin
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string name;
   int i;

   for (i=1; i <=3; i++)
   {
       cout <<"Enter a string => ";
       cin >> name;
       cout << endl << name << endl;
   }
   return 0;
}
Enter a string => stringone	tabstringtwo

stringone
Enter a string => 
tabstringtwo
Enter a string =>    spacespacespace

spacespacespace
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string name;
   int i;

   for (i=1; i <=3; i++)
   {
       cout <<"Enter a string => ";
       getline(cin, name);
       cout << endl << name << endl;
   }
   return 0;
}
Enter a string => this is a string of words

this is a string of words
Enter a string => oneword	tab	tab

oneword	tab	tab
Enter a string => x

x
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string w1, w2;
   int i, num;

   
   for (i=1; i <=3; i++)
   {
       cout <<"Getline a word => ";
       getline(cin, w1);
       cout << endl << w1 << endl;
       cout <<"cin a word => ";
       cin >> w2;
       cout << endl << w2 << endl;
   }
   return 0;
}
Getline a word => wordOne wordTwo

wordOne wordTwo
cin a word => wordOne wordTwo

wordOne
Getline a word => 
 wordTwo
cin a word => wordOne

wordOne
Getline a word => 

cin a word => OOPS

OOPS