0

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?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Daniel Jaló
  • 127
  • 1
  • 3
  • 11
  • Can you include the function declaration? What does it return? – charlieparker Mar 06 '14 at 02:36
  • What do you mean by function declaration? When I run the code I only receive a 'floating point exception' output, even tho the code compiles. – Daniel Jaló Mar 06 '14 at 02:38
  • Never mind, I see it there. I had it scrolled down too far. It's the first line of the code you posted. – charlieparker Mar 06 '14 at 02:39
  • How can "I get no error" and "I get the output floating point exception" both be true? Can you share any details about the exception? – Scott Hunter Mar 06 '14 at 02:41
  • I'm compiling it with g++, in linux. When I compile it no error appears. The `floating point exception` is the output I get when I run it in the console. All the previous code is working tho, which in my case is simply a cout message, "Hello", to check if it worked until that point. @ScottHunter – Daniel Jaló Mar 06 '14 at 02:45
  • your memory allocation scheme in this is flat-wrong. What does "composed of only numbers" *mean* ? Is the entire file solid *digits* ? There are whites-space separated floating point values? Integers? A specific number per of each/either per line? And before you do anything else, [read why `.eof()` as a loop condition in C++ is wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). If your prof authored this code, I'd drop the class faster than it took to write this comment. – WhozCraig Mar 06 '14 at 02:51
  • There are white-spaces separating the integer numbers in the .txt file. There are 480 lines, each having 640 columns (filled with positive integer numbers) @WhozCraig – Daniel Jaló Mar 06 '14 at 03:00
  • `while (SamplesFile.getline(s, Counter))` Counter is int.. is it really compiling....:-o – HadeS Mar 06 '14 at 03:20
  • Oh man. This code is so horrible and full of major bugs, I feel sorry for you if it has been provided to you to work with. – M.M Mar 06 '14 at 05:57

1 Answers1

0

You can't use .eof() thats bad and is normallly from say C# i would say try while(sampleFile.good()) and go from there.

Josh
  • 173
  • 10
  • 1
    `while(sampleFile.good())` isn't much better than using `.eof()`, the issue is that the flags are set AFTER a failed read, http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong. – Cramer Mar 06 '14 at 05:10