Retrieving data from a file (.dat)

I am new to fstream, and I need help ASAP!

My goal is to make a program that opens a .dat file and it checks to see if the number exists in the file. If it does, it tells where that number is. I need a very simplified C++ answer, because I am a beginner and new to this. Then, I need a program that reads the entire data file and tells the user whether each number is even or odd, and then totals them. I need help ASAP. Thank you in advance! Here is my file so far...please finish!

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int value;
ifstream number_file;
cout << endl << "Opening file: numbers.dat..." << endl;
number_file.open("numbers.dat");

if (number_file.fail())
{
cout << "Failed to open file." << endl; exit(1);
}

cout << "Enter a numerical value to see if it exists in that file: ";
cin >> value;
int count = 0;
do {
cout << endl << count << " " << value;
count++;
} while (number_file >> value);
cout << endl;
number_file.close();
return 0;
}
Load the entire file into an array or vector and then use the find() algorithm.

I know this isn't the best way but it is a quick and dirty solution.
Last edited on
Topic archived. No new replies allowed.