07-20-2011, 10:58 PM
Read data from file into arrays in c++ 2011
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
using namespace std;
const string AUTHOR = "Rf";
const string ROLE = ", (s)\n";
const string SOLTXT = "\nSolution by ";
const string INFILE_ERR_MSG = "File does not exist or it is empty!\n";
const int EXIT_ERR_INFILE = -1; // Error with input file
const int MAX_INT = 500;
bool openFileForInput(ifstream&);
int main()
{
ofstream outData("INTEGERS.TXT");
time_t t;
time(&t);
srand (t);
int base = -250;
int num;
for (int i = 0; i < MAX_INT; i++)
{
outData << (num = base + rand()%500) << endl;
}
outData.close();
ifstream inData;
// Open a file for input
// Check for existence of file in current directory
// If file exists, is there any data in the file
if (!openFileForInput(inData))
{
cout << INFILE_ERR_MSG;
system("PAUSE");
return EXIT_ERR_INFILE;
}
cout << endl << endl;
system ("PAUSE");
return 0;
}
bool openFileForInput(ifstream& inp)
{
char check;
inp.open("INTEGERS.TXT"); // See if we can open
check = inp.peek(); // Determine whether file is not empty
if (inp && !inp.eof() ) // checks if the file exists and not empty
return true;
return false;
} /* End of openFileForInput() */
bool getNum(ifstream& inp, ofstream& otp)// this is where I'm stuck
{
int num;
unsigned int neg[];
int posEven[];
int posOdd[]
inp >> num;
if (inp && !inp.eof())
{
cin >> inp >> int posEven[num]
return true;
}
return false;
}0300-9520262
Get solution in 24 hour |cpmputer & internet
mileys81:[SIZE="4"][SIZE="3"]....[/SIZE][/SIZE]
