0

I'm working on a project. The idea is that it has two input files, we'll call them TimeFile and FullFile.

TimeFile gives two timestamps in the format:

DD-MM-YY HH:MM:SS     DD-MM-YY HH:MM:SS

And FullFile is in the format of a timestamp and some data:

YYYY-MM-DD HH:MM:SS   Value    Error    Error

The idea is that the program reads the timestamps from TimeFile, then goes through all the lines in FullFile, and if it finds a timestamp from FullFile that lands between two from TimeFile, it copies the whole line into a new smaller data file. In essence, I want to go from one gigantic data file to a bunch of smaller data files divided up by time intervals.

Needless to say, it doesn't work. It does most of what I want, but the resulting smaller data files are always empty.

The strange part is why. It appears to read the timestamps from TimeFile just fine, but it screws up reading from FullFile and just reads the first line over and over again. I don't really have a solid idea why either, best I can determine is that they're reading the same way, but one works and the other doesn't.

#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <tuple>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <complex>
#include <stdint.h>
#include <time.h>
#include <string.h>

int main(){

  //Read Run-Times-File - Cycle 1
  std::ifstream TimeFile;
  TimeFile.open("jet_run_times.dat");
  if(!TimeFile.good()){
    std::cout << "TimeFile Didn't Work" << std::endl;
    return 1;
  }
    
  //Read Full-File - Cycle 2
  std::ifstream FullFile;
  FullFile.open("jet_full.txt");
  if(!FullFile.good()){
    std::cout << "FullFile Didn't Work" << std::endl;
    return 1;
  }
  std::cout << "Both Files Worked" << std::endl;

  std::ofstream results; 
  
  //Initial Time - A ; Final Time - B ; Jet Pressure Time - C

  int iday, ihour, imin, isec, fday, fhour, fmin, fsec, imon, fmon;
  char dash, colon ;
  std::string imonth, fmonth ;
  //std::ostringstream temp;
  int run = 0;
  int year, month, day , hour, min;
  double sec, value, neg, pos, AfterStart, BeforeEnd;

  for(int i = 0 ; i < 192 ; i++) {
  //Cycle 1
  //Write jet_run_XXX.txt
    run++;
  
    std::ostringstream temp;
  
    if(run <= 9) temp << "jet_run_00" << run << ".txt" ;
    if( (run >= 10) && (run <= 99) ) temp << "jet_run_0" << run << ".txt" ;
    if(run >= 100) temp << "jet_run_" << run << ".txt" ;
    results.open(temp.str());
    std::cout << temp.str() << " File Made" << std::endl;
    
    TimeFile >> iday >> dash >> imonth  >> ihour >> colon >> imin >> colon >> isec  >> fday >> dash >> fmonth >> fhour >> colon >> fmin >> colon >> fsec;

    /*
    std::cout << "iday " << iday << std::endl;
    std::cout << "dash " << dash << std::endl;
    std::cout << "imonth " << imonth << std::endl;
    std::cout << "ihour " << ihour << std::endl;
    std::cout << "colon " << colon << std::endl;
    std::cout << "imin " << imin << std::endl;
    std::cout << "isec " << isec << std::endl;
    std::cout << "fday " << fday << std::endl;
    std::cout << "dash " << dash << std::endl;
    std::cout << "fmonth " << fmonth << std::endl;
    std::cout << "fhour " << fhour << std::endl;
    std::cout << "colon " << colon << std::endl;
    std::cout << "fmin " << fmin << std::endl;
    std::cout << "fsec " << fsec << std::endl;
    */

    if( imonth == "Apr-22") imon = 4;
    if( fmonth == "Apr-22") fmon = 4;
    if( imonth == "May-22") imon = 5;
    if( fmonth == "May-22") fmon = 5;
    
  /* 
     std::cout << "imon " << imon << std::endl;
     std::cout << "fmon " << fmon << std::endl;
  */

  //Cycle 2
    
    for(int j = 0 ; j < 5833 ; j++){

      FullFile >> year >> dash >> month >> dash >> day >> hour >> colon >> min >> colon >> sec >> value >> neg >> pos;
      std::cout << j << std::endl; 
      
      /*    
    std::cout << "year " << year << std::endl;
    std::cout << "dash " << dash << std::endl;
    std::cout << "month " << month << std::endl;
    std::cout << "dash " << dash << std::endl;
    std::cout << "day " << day << std::endl;
    std::cout << "hour " << hour << std::endl;
    std::cout << "colon " << colon << std::endl;
    std::cout << "min " << min << std::endl;
    std::cout << "sec " << sec << std::endl;
    std::cout << "value " << value << std::endl;
    std::cout << "neg " << neg << std::endl;
    std::cout << "pos " << pos << std::endl;  
      */

  //Set-Up the Check if  A <= C <= B
      AfterStart = (sec - isec) + (min - imin)*100 + (hour - ihour)*10000 + (day - iday)*1000000 + (month - imon)*100000000;   
      BeforeEnd = (fsec - sec) + (fmin - min)*100 + (fhour - hour)*10000 + (fday - day)*1000000 + (fmon - month)*100000000;

      std::cout << "AfterStart " << AfterStart << std::endl;
      std::cout << "BeforeEnd " << BeforeEnd << std::endl;

  //If A <= C <= B, copy all of C to jet_run_XXX.txt
      if ( (AfterStart >= 0.0) && (BeforeEnd >= 0.0) ){
    results << year << dash << month << dash << day << "  " << hour << colon << min << colon << sec << "  " << value << "  " << pos << "  " << neg << '\n' <<  std::endl;
    std::cout << "Got One!" << std::endl;
      }
    }
    //End Cycle 2
    results.close();
  }

  //End Cycle 1
  return 0;
}

I'm stumped, any help would be appreciated.


Edit: Thinking it might help if I give a few lines of each of my data files to see if it's a formatting issue, so here's the first 3 lines of each:

TimeFile

03-Apr-22 22:42:19  03-Apr-22 22:56:13
03-Apr-22 22:58:25  03-Apr-22 23:15:14
03-Apr-22 23:17:23  03-Apr-22 23:35:32

FullFile

2022-04-13 12:39:37.500000000   70.00000    0.0 0.0
2022-04-13 12:43:52.500000000   70.00000    0.0 0.0
2022-04-13 12:48:07.500000000   70.00000    0.0 0.0

Edit2:- Important discovery. Running this code with that block uncommented out reproduces the data in the line but instead of 70 0 0 you get 1 0 43.1495 for the last three values. GetLine doesn't seem to do this but I'm having trouble understanding how to cut that open.

Not entirely sure what this means beyond that somehow that's not reading those values properly but when I try to account for a tab it's not an improvement.

Jason
  • 11
  • 3
  • You should always check the result from input operations. `if (FullFile >> year >> dash >> month >> dash >> day >> hour >> colon >> min >> colon >> sec >> value >> neg >> pos) { /* process data */ } else { /* handle error */ }` An undetected error reading is a common reason for data to repeat. – Retired Ninja May 23 '22 at 19:46
  • Thought I did, that's what the block of commented out code below that reading is for, and it was working fine when it was in. – Jason May 23 '22 at 19:48
  • Well, if you read all 192 lines worth of data and manually matched them up to the input then I suppose you don't have a problem, but wouldn't it be easier to let the code tell you there was a problem? Gets a little harder when there's 5833 lines to manually check doesn't it? – Retired Ninja May 23 '22 at 19:50
  • And because you got me thinking I checked it again, and when I uncomment it I do see repeating. The values are all correct for the first line but it just repeats it. Alright, that helps but still trying to think of how to fix it. – Jason May 23 '22 at 19:53
  • Alright, just did a test with it repeating and displaying whole lines and that appears to work correctly. So maybe there's an invisible character at the end of the line that isn't working? – Jason May 23 '22 at 20:47

0 Answers0