#include #include #include #include using namespace std; int main() { string filename("unsorted_keywords.txt"); ifstream fin(filename); string tempString; char tempCstring[64]; if (!fin) { if (!fin) { cout << "Unable to open input file: " << filename << endl; exit(1); } } /* /////////// The string approach ////////////// // read and print the first 5 records using >> for (int i = 0; i < 5; i++) { fin >> tempString; cout << '/' << tempString << '/' << endl; } fin.seekg(0); // go to the top of the input file cout << endl; // read and print the first 5 records using getline (string version) for (int i = 0; i < 5; i++) { getline(fin,tempString); if (tempString.back() == '\r') tempString.pop_back(); cout << '/' << tempString << '/' << endl; cout << tempString << '/' << endl; } */ /////////// The C-string approach ////////////// fin.seekg(0); // go to the top of the input file // read and print the first 5 records using >> for (int i = 0; i < 5; i++) { fin >> tempCstring; cout << '/' << tempCstring << '/' << endl; } fin.seekg(0); // go to the top of the input file cout << endl; // read and print the first 5 records using getline (string version) for (int i = 0; i < 5; i++) { fin.getline(tempCstring,sizeof(tempCstring)); strtok(tempCstring,"\r"); cout << '/' << tempCstring << '/' << endl; cout << tempCstring << '/' << endl; } }