[sclug] Yet another c++ question

Tim Sutton suttont at onetel.net.uk
Sat Oct 25 09:05:38 UTC 2003


Ok this one confuses me:

cout << "Test : " << (10388820/108262440)*100 << endl;

Test : 0

Yet the same expression in gnumeric gives the more expected: 9.59

I did the above to debug a little formula that I have that is supposed to 
measure how far I am through a file I am parsing. But it always returns 0 ! 
:-( What gives?  More complete listing provided below with the problem bit 
marked with //*******

TIA

Tim

--------------

/** Use the header info for a given file type to determine the begining of the 
data block and position the
*    dataStartFPos there. This method will need to be called explicitly by the 
client app so that when multiple
*   copies of the same file are being opened, we dont need to do the same 
thing each time.*/
   std::vector <fpos_t>* FileReader::getBlockMarkers()
{
    //
    // Set up some vars
    //

    char myLineChar[65535];  //temporary holder for fgetted lines
    fpos_t myFilePos; //store the current position in the file
    long myMatrixRowsLong;
    long myFileSizeLong;
    long myFilePosLong;  //where we are in the currently open file
    myFilePosLong=0;
    //clear the vector
    dataBlockMarkersVector.clear();
    //find out how long the file is
    fseek(filePointer,0,SEEK_END);
    myFileSizeLong = ftell(filePointer);
    rewind(filePointer);
    //
    // Start parsing the file
    //
                   
    cout << "FileReader::getBlockMarkers() - moving to the start of the file" 
<< endl;
    fseek( filePointer,0, SEEK_SET);
    cout << "FileReader::getBlockMarkers() - skipping " << headerLinesInt << " 
file header line(s)" << endl;
    for (int i=1; i <= headerLinesInt; i++)
    {
          //read an impossibly long line - fgets will stop if it hits a 
newline
          fgets (myLineChar, 65535, filePointer);
          cout << myLineChar ;
    }

    //Calculate number of rows in a month (depends on FileType)
    myMatrixRowsLong = ((xDimLong * yDimLong) / columnsPerRowLong);
    cout << "FileReader::getBlockMarkers() - looping through rest of file with 
month header size of " << monthHeaderLinesInt << " lines and data block size 
of " << myMatrixRowsLong << " lines." << endl;    
    //loop through the rest of the file getting the start pos for each 
datablock
    do 
    {
          cout << "FileReader::getBlockMarkers()  getting header block " << 
endl;
          //skip the datablock headers
          if (monthHeaderLinesInt > 0)
          {
              for (int i=1; i <= monthHeaderLinesInt; i++)
              {
                  //read an impossibly long line - fgets will stop if it hits 
a newline
                 fgets (myLineChar, 65535, filePointer);
                 cout << myLineChar;
              }
          }
          else
          {
              cout << "no datablock header" << endl;
          }
          cout << "FileReader::getBlockMarkers()  getting data block " << 
endl;
          //so the file pointer is now at the start of the datablock - add it 
to the vector
          fgetpos(filePointer, &myFilePos);
          dataBlockMarkersVector.push_back(myFilePos);
          //now skip the data objects for this datablock
          for (int i=1; i <= myMatrixRowsLong; i++)
          {
              //read an impossibly long line - fgets will stop if it hits a 
newline
              fgets (myLineChar, 65535, filePointer);
              if (i==1) cout << myLineChar; //print out the first line of each 
datablock

          }
          //calculate where we are in the file and update the progress member 
var

          myFilePosLong = ftell(filePointer);
	 // ****** The following lines unexpectedly return 0 always ?! ********** //
          taskProgressInt =  ( myFilePosLong / myFileSizeLong) * 100 ;
          cout << "Task Progress: " << ( myFilePosLong / myFileSizeLong) * 100 
<< endl;
          cout << "Test : " << (10388820/108262440)*100 << endl;
          cout << "Position " << myFilePosLong << "/" << myFileSizeLong << " 
("
               << taskProgressInt << ") : ";
          for (int i=1;i<(taskProgressInt/10);i++) cout << "*";
          cout << endl;
          
          
          
    }    while (!feof(filePointer)) ;

    
    
    cout << "FileReader::findDataStart() - read markers for " << 
dataBlockMarkersVector.size() << " data block(s)" << endl;
    cout << "FileReader::findDataStart() - moving back to the start of the 
file" << endl;
    fseek( filePointer,0, SEEK_SET);
    cout << "FileReader::findDataStart() - finished - returning vector of 
datablock start positions" << endl;
  
    return  &dataBlockMarkersVector;
}
-- 
Get my public keys from:

http://tim.suttonfamily.co.uk/modules.php?name=Content&pa=showpage&pid=2




More information about the Sclug mailing list