I'm working on a little script that requires me to read a .txt file, composed of only numbers. I'm not that familiar with C++, therefore my professor gave us the following function, which should allow us to read a .txt file:
(I am sorry for putting the whole function here, but because of my lack of knowledge I can't write a minimal example.)
int * ReadDataSample(const char * filename,int * NoSamples, int * NoApcs){
    ifstream SamplesFile;
int Counter = 0;
float data = 0;
int  * p = NULL;
char * s = NULL;
SamplesFile.open(filename,ios::in);
if(SamplesFile.is_open())
{
    while(!SamplesFile.eof())
    {
        SamplesFile >> data;
        Counter++;
    }
    SamplesFile.clear();
    SamplesFile.seekg (0, ios::beg);
    // read line
    s = new (nothrow) char[Counter];
    if(s == 0) { cout <<"Error: opening file for reading - could not allocate memory..." << endl; exit(EXIT_FAILURE); }
    (*NoSamples) = 0;
    // count number of samples in s 
    while (SamplesFile.getline(s, Counter))
    {
            ++(*NoSamples);
    }
    (*NoApcs) = Counter/(*NoSamples);
    SamplesFile.clear();
    SamplesFile.seekg (0, ios::beg);
    delete [] s;
}else{ cout << " Error opening file... exiting"<< endl; exit(EXIT_FAILURE);}
// allocate memory...
if(Counter != 0)
{   
    p = new (nothrow) int [Counter];
    if(p == 0) { cout <<"Error: opening file for reading - could not allocate memory..." << endl; exit(EXIT_FAILURE); }
    for(int i = 0; i < Counter && SamplesFile.eof()!=true; i++)
    {
        SamplesFile >> data;
        p[i] = (int)data;
    }
    return p;
}else{
    return NULL;
}
}
I try to call this function by first initiating a pointer to an int:
int NoLines = 480, NoColumns = 640;
int * p;
p = ReadDataSample("file.txt",& NoLines, & NoColumns);
However, when I run the code I get no error but I get the output floating point exception in the console. Right now the only thing I have on my main is a cout with a message.
If anyone could point me in the right direction I'd appreciate.
edit:
Since everyone seems to agree that the code given to me right now isn't the best option I decided to create a new one, using info gathered from the web. What I have is this:
void grabI(Qstruct & q){
cout << "in grabI" << endl;
int counter = 0;
int i = 0;
ifstream myfile;
myfile.open("data.txt",ios::in);
if(myfile.is_open()){
    while(!myfile.eof()){
        myfile >> i;
        counter++;
    }
counter--;
myfile.close();
myfile.open("data.txt",ios::in);    
for(i=0;i<counter;i++){
    myfile >> q.I[i];   
}
}
else{
    cout << "Unable to read file." << endl;
}   
myfile.close(); 
}
It works as intended. Is there anything I should correct in it, or can I just leave it this way?
 
     
    