简体   繁体   中英

Reading a .txt file with C++

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?

您不能使用.eof()那很糟糕,通常是从说C#开始,我会说尝试while(sampleFile.good())并从那里去。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM