Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <math.h>
- #include <fstream>
- #include <conio.h>
- /**
- * This briefly demonstrates writing a file in C++
- * using a console application. It will take in a line
- * text and put that line in a file.
- *
- * Asks how many entries into a text file and asks for
- * some details. There is also something to assign tasks
- * to the 'entered' user. This was an example script for
- * some Coventry University students who had an arbitrary
- * set by some academic to show file handling.
- *
- * @Author: Shaun B
- * @Version: 1.0.2 - 2013-01-09
- * @Todo:
- *
- **/
- int main();
- int retrieve();
- void clearKeyboardBuffer();
- using namespace std;
- /**
- * Main entry of example
- * @author Shaun B
- * @version 0.0.1
- */
- int main()
- {
- string name = "\0";
- string dob = "\0";
- string age = "\0";
- int numberOfPeople = 0;
- int numberOfTasks = 0;
- int i = 0;
- string buffer = "\0";
- string myTextFile = "\0";
- ofstream myfile ("tester.txt");
- cout << "Record system v1.0." << endl;
- cout << "How many records would you like to create?" << endl;
- cin >> numberOfPeople;
- if (numberOfPeople>0)
- {
- for (i=0; i<numberOfPeople; i++)
- {
- //clearKeyboardBuffer();
- cout << "Please enter the users name for record: " << i+1 << endl;
- getline(cin, buffer);
- myTextFile.append(buffer);
- myTextFile.append("\n");
- cout << "Please enter their date of birth (DD/MM/YY)" << endl;
- getline(cin, buffer);
- myTextFile.append(buffer);
- myTextFile.append("\n");
- cout << "What is your favourite colour?"<< endl;
- getline(cin, buffer);
- myTextFile.append(buffer);
- myTextFile.append("\n");
- cout << "What is your favourite sport?" << endl;
- getline(cin, buffer);
- myTextFile.append(buffer);
- myTextFile.append("\n");
- cout << "Please enter their age" << endl;
- cin >> age;
- myTextFile.append(age);
- myTextFile.append("\n");
- cout << "How many tasks would you like to set? (Zero for none)" << endl;
- cin >> numberOfTasks;
- if (numberOfTasks>0)
- {
- for (int x = 0; x < numberOfTasks; x++)
- {
- clearKeyboardBuffer();
- cout << "Please enter task " << x+1 << endl;
- getline(cin, buffer);
- if(!buffer.empty())
- {
- myTextFile.append(buffer);
- }
- buffer.clear();
- cout << "Is task number " << x+1 << " a priority task (Y/N)" << endl;
- cin >> buffer;
- if(!buffer.substr(0,1).compare("Y") || !buffer.substr(0,1).compare("y"))
- {
- myTextFile.append(" !Important");
- }
- myTextFile.append("\n");
- }
- }
- else
- {
- cout << "No tasks set for this person." << endl;
- }
- myTextFile.append("!END\n");
- }
- }
- myTextFile.append("\0");
- buffer.clear();
- try
- {
- if(myTextFile.length()>=1)
- {
- cout << "Creating file: tester.txt" << endl;
- myfile << myTextFile;
- }
- myfile.close();
- }
- catch (int e)
- {
- cout << "An exception has occurred in your programme." << endl;
- }
- retrieve();
- return 0;
- }
- /**
- * Retrieves line from file
- * @author Shaun B
- * @version 0.0.1
- */
- int retrieve ()
- {
- string line = "\0";
- string text = "\0";
- string wholeFile = "\0";
- string searchInFile = "\0";
- string answer = "\0";
- bool found = false;
- int counter = 0;
- int len = 0;
- clearKeyboardBuffer();
- ifstream myfile ("tester.txt");
- if (myfile.is_open())
- {
- while ( myfile.good() )
- {
- cout << "What user name do you want to search for?" << endl;
- getline(cin, searchInFile);
- len = searchInFile.length();
- while (getline (myfile,line))
- {
- text.append(line);
- int i = 0;
- while(i+len <= text.length())
- {
- if (!text.substr(i,i+len).compare(searchInFile))
- {
- cout << "User found! " << endl;
- found = true;
- cout << "Would you like to view the users details (Y/N)?" << endl;
- cin >> answer;
- if(!answer.substr(0,1).compare("Y") || !answer.substr(0,1).compare("y"))
- {
- cout << "Details:" << endl;
- while(text.compare("!END"))
- {
- cout << text << endl;
- text.clear();
- getline(myfile,line);
- text.append(line);
- }
- }
- }
- i++;
- }
- counter++;
- text.clear();
- }
- }
- if (!found)
- {
- cout << "User was not found." << endl;
- }
- }
- else
- {
- cout << "Unable to open file";
- return -1;
- }
- myfile.close();
- return 0;
- }
- /**
- * clears keyboard buffer
- *
- * @author Shaun B
- * @version 0.0.2
- * @fixed 15-01-2016
- */
- void clearKeyboardBuffer()
- {
- while (_kbhit())
- {
- _getche();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement