I have a strange number input problem.

I am scanning in numbers from a file, like so:

While(bla){
double temp;
minmaxFS >> temp;
//do crap
}

And it works fine until it reaches a number like:

8e-05

(A non-decimal exponential number). Then, it just hangs up and scans 0 over and over and over again.

It works fine on decimals integers (8.993e-05).

Is there anyway I can fix this? Help! Thanks!

Recommended Answers

All 10 Replies

Paraphrasing code does nothing for me -- show me actual code and I can give actual help. My best guess as to what you are asking would be like this:

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

int main()
{
   ifstream file("file.txt");
   double value;
   while ( file >> value )
   {
      cout << "value = " << value << '\n';
   }
   return 0;
}

/* file.txt
8e+05
8e-05
8.993e-05
*/

/* my output
value = 800000
value = 8e-05
value = 8.993e-05
*/

But I am not seeing the issue.

Ok, here is my full function:

int getOLines()
{

        statsFS >> statLine.id >> statLine.primary_type >> statLine.secondary_type >> statLine.period >> statLine.epochnumber >> statLine.savedextremenumber;
        minmaxFS >> minmaxLine.id;
        while(statLine.id != minmaxLine.id)
        {
                double temp;
                minmaxFS >> temp;
                if (temp == statLine.id)
                        minmaxLine.id = temp;
                if (minmaxFS.eof())
                {
                        errorFS <<"Error inserting at: "<<statLine.id<<endl;
                        return 0;
                        }
        }

        shotsFS >> shotsLine.id;
        shotsFS.getline(shotsLine.data, 500);

        if(statLineI.id != shotsLineI.id)
        {
                errorFS << "Matchup error at:" << statLineI.id << endl;
                return 0;
        }

        if(statsFS.eof())
                return 0; // all done!
        else
                return 1;

}

Here are the global variables:

struct statLine{
int id;
int primary_type;
int secondary_type;
double period;
double epochnumber;
int savedextremenumber;
}statLine, statLineI;

struct minmaxLine{
int fn;
int id;
double time;
double value;
double type;
double integral;
}minmaxLine, minmaxLineI;

struct shotsLine{
int id;
char data[500];
}shotsLine, shotsLineI;

ofstream errorFS("inserter_error.txt");
ifstream statsFS, minmaxFS, shotsFS;
ofstream statsFSO, minmaxFSO, shotsFSO;

Here is what a sample chunk of my input file looks like (a chunk that has problems).

(minmaxFS points to this file)

522242053
0.00088 0.01556596 1 0.1319475
522242054
8e-05 0.01557778 1 0.1311683
522242055
0.00052 0.0155896 1 0.1304501

Any help is greatly appreciated!

I'm sorry, I just don't have the time to try to rebuild a test program today. A quick attempt was this.

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

struct statLine
{
   int id;
   int primary_type;
   int secondary_type;
   double period;
   double epochnumber;
   int savedextremenumber;
}statLine, statLineI;

struct minmaxLine
{
   int fn;
   int id;
   double time;
   double value;
   double type;
   double integral;
}minmaxLine, minmaxLineI;

struct shotsLine
{
   int id;
   char data[500];
}shotsLine, shotsLineI;

ofstream errorFS("inserter_error.txt");
ifstream statsFS, minmaxFS, shotsFS;
ofstream statsFSO, minmaxFSO, shotsFSO;

int getOLines()
{

   statsFS >> statLine.id >> statLine.primary_type >> statLine.secondary_type >> statLine.period >> statLine.epochnumber >> statLine.savedextremenumber;
   minmaxFS >> minmaxLine.id;
   while ( statLine.id != minmaxLine.id )
   {
      double temp;
      minmaxFS >> temp;
      if ( temp == statLine.id )
         minmaxLine.id = temp;
      if ( minmaxFS.eof() )
      {
         errorFS <<"Error inserting at: "<<statLine.id<<endl;
         return 0;
      }
   }

   shotsFS >> shotsLine.id;
   shotsFS.getline(shotsLine.data, 500);

   if ( statLineI.id != shotsLineI.id )
   {
      errorFS << "Matchup error at:" << statLineI.id << endl;
      return 0;
   }

   if ( statsFS.eof() )
      return 0; // all done!
   else
      return 1;

}

int main()
{
   return 0;
}

Obviously, it doesn't do much.

And I copied the snippet of a sample file. But to where and how that is involved I don't know. And is the beginning portion of the file significant since some input routines differ from the loop?

Oh, I don't mean for you to write the entire test code out. I thought there might be some easy and obvious error in my code.

But, if you are feeling especially generous, I've attached my code and some truncated data files.

Just run the program with the parameter "73" (aka, inserter 73) and you'll see my error.

Thanks!


Overview of the program:

Basically, just merges two sets of data files into one file in order.

Oh, I don't mean for you to write the entire test code out. I thought there might be some easy and obvious error in my code.

I like to use tools such as lint and the compiler to point me to the obvious. And I like to make sure I see the problem you see before I start pointing at portions of code. (Because often a bug is not where you first look.)

Thanks for the test code and data. I'm taking a quick peek, to try to repeat the error. I've gotten to this point, where is gives a "blank stare"

...
Processing: 522231052 M: 522440235
Processing: 522231053 M: 522440235

I'll poke it some more from time to time.

Yeah, at the point in the process, it is reading from "73_minmax.dat" and comes across the following:

522231052
2.121277e-311 0.01556361 1 0.1181511
522231053
[B]4e-05[/B] 0.01557543 1 0.1182544
522231054
2.121346e-311 0.01558724 1 0.1185762

I've stepped through the code with my compiler and it chokes when it trys to scan in "4e-05". After hitting that point, it just scans in "0" over and over and over - never progressing in the file (and never reaching eof).

I don't know if this is part of the issue, but changing

if ( minmaxFS.eof() )

to

if ( !minmaxFS ) // [edit] was backwards logic

ends the blank stare.

I've never been fond of using eof() for loop control.

[edit]Oops. Didn't see your reply.

Edited my last post. To avoid confusion, a new reply is in order.

The following

if ( !minmaxFS )
      {
         if (minmaxFS.bad())
         {
            cerr << "bad" << endl;
         }
         else if (minmaxFS.eof())
         {
            cerr << "eof" << endl;
         }
         else if (minmaxFS.fail())
         {
            cerr << "fail" << endl;
         }
         errorFS <<"Error inserting at: "<<statLine.id<<endl;
         return 0;
      }

yields this.

...
+++++Processing: 522231053 M: 522440235
+fail

So now I need to learn what fail means. :)

I was able to fix it by skirting the issue.

I scanned my numbers in as char *'s and then atoi()'ed them into doubles.

It's a bit awkward and doesn't address the issue at all, but it works!
Thanks for your help!

I'm glad you've got a workaround, but I'll mention a few things.

I scanned my numbers in as char *'s and then atoi()'ed them into doubles.

A "to integer" function to get a double?

Those are awfully small numbers.

522231052
2.121277e-311 0.01556361 1 0.1181511
522231053
4e-05 0.01557543 1 0.1182544
522231054
2.121346e-311 0.01558724 1 0.1185762

In my <float.h>, I have this.

#define DBL_MIN             2.2250738585072014E-308

I doesn't seem that making a few changes about this solved anything, but I think you ought to take note of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.