输入文件读数只需重复2个文件中的1个数据行

发布于 2025-01-31 17:31:58 字数 6840 浏览 3 评论 0原文

我正在从事一个项目。这个想法是它有两个输入文件,我们将其称为TimeFilefullfile

timefile给出了格式的两个时间戳:

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

fullfile以时间戳的格式和一些数据:

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

这个想法是该程序从timefile timefile < /code>,然后浏览fullfile中的所有行,如果它从fullfile中找到一个时间戳,该时间戳timefile中降落在两个之间将整行复制到一个新的较小数据文件中。从本质上讲,我想从一个巨大的数据文件到一堆较小的数据文件,除以时间间隔。

不用说,它行不通。它可以完成我想要的大部分内容,但是所产生的较小数据文件始终是空的。

奇怪的部分是原因。它似乎可以从timefile读取时间戳,但是它会从fullfile中弄清楚读数,然后一遍又一遍地读取第一行。我真的没有一个扎实的想法,为什么我能确定的是他们以同样的方式阅读,但是一个作品,另一个作品却没有。

#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;
}

我很难过,任何帮助都将不胜感激。


编辑:认为,如果我给出每个数据文件的几行以查看是否是格式化问题,这可能会有所帮助,因此,这是每条数据的前3行:

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: - 重要发现。用该块未注销的块运行此代码会重现该行中的数据,但没有70 0 0,您可以在最后三个值中获得1 0 43.1495。 Getline似乎没有这样做,但是我很难理解如何切开这一开放。

不完全确定这是什么意思,除了以某种方式无法正确读取这些值,但是当我尝试考虑一个标签时,这并不是一个改进。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文