用 find 解析文件会给出不同文件的奇怪结果

Posted

技术标签:

【中文标题】用 find 解析文件会给出不同文件的奇怪结果【英文标题】:Parsing file with find gives strange results with different files 【发布时间】:2020-10-16 09:59:04 【问题描述】:

我之前尝试过问这个问题,并且正确地被告知我需要提供一个完整的工作示例,以便其他人可以重现我的结果并提供指导。

这是我用于解析文件的代码的缩短版本。 readFileToStrings 的重点是将文件保存为基于模式[Atoms][GTO][MO] 的单独字符串。

#include 
#include 
#include 
#include 

void readFileToStrings(const std::string& 文件名, std::string& ParticlesString,
                       std::string& 高斯字符串,std::string& MOString) 
    std::ifstream 源(文件名);
    std::string 行;
    while(std::getline(source, line)) 
        if(line.find("Molpro") != std::string::npos) 
            std::getline(source, line, '['); // 读取直到 [
        
        if(line.find("Atoms") != std::string::npos) 
            std::getline(source, line, '['); // 读取直到 [
            ParticlesString.append(line);
        
        if(line.find("GTO") != std::string::npos) 
            std::getline(source, line, '['); // 读取直到 [
            GaussianString.append(line);
        
        if(line.find("MO") != std::string::npos) 
            std::getline(source, line, '\0'); // 一直读到文件结束
            MOString.append(line);
        
    


int main(int argc, char** argv) 
    如果(argc!= 2)
        std::cerr \n";
        返回 1;
    
    std::string filename(argv[1]), ParticlesString, GaussianString, MOString;
    readFileToStrings(filename, ParticlesString, GaussianString, MOString);

    std::cout 

我需要从不同版本的程序中解析两个不同的文件。他们来了

文件 1:

[Molden Format]
[Atoms] (AU)
O    1    8         0.000000000000       0.000000000000      -0.124302651265
H    2    1         0.000000000000      -1.424704710000       0.986386639735
H    3    1        -0.000000000000       1.424704710000       0.986386639735
[GTO]
  1 0
 s    8  1.00
    11720.0000000000         0.0007100000
     1759.0000000000         0.0054700000
      400.8000000000         0.0278370000
      113.7000000000         0.1048000000
       37.0300000000         0.2830620000
       13.2700000000         0.4487190000
        5.0250000000         0.2709520000
        1.0130000000         0.0154580000
 s    8  1.00
    11720.0000000000        -0.0001600000
     1759.0000000000        -0.0012630000
      400.8000000000        -0.0062670000
      113.7000000000        -0.0257160000
       37.0300000000        -0.0709240000
       13.2700000000        -0.1654110000
        5.0250000000        -0.1169550000
        1.0130000000         0.5573680000
 s    1  1.00
        0.3023000000         1.0000000000
 p    3  1.00
       17.7000000000         0.0430180000
        3.8540000000         0.2289130000
        1.0460000000         0.5087280000
 p    1  1.00
        0.2753000000         1.0000000000
 d    1  1.00
        1.1850000000         1.0000000000

  2 0
 s    3  1.00
       13.0100000000         0.0196850000
        1.9620000000         0.1379770000
        0.4446000000         0.4781480000
 s    1  1.00
        0.1220000000         1.0000000000
 p    1  1.00
        0.7270000000         1.0000000000

  3 0
 s    3  1.00
       13.0100000000         0.0196850000
        1.9620000000         0.1379770000
        0.4446000000         0.4781480000
 s    1  1.00
        0.1220000000         1.0000000000
 p    1  1.00
        0.7270000000         1.0000000000

[5D]
[9G]

[MO]
 Sym= A1
 Ene= -2.05504193878478176e+01
 Spin= Alpha
 Occup=  2.00000000000000000e+00
  1  1.00090233711673782e+00
  2  1.18218569881159419e-03
  3 -2.65616439031917534e-03
  4  0.00000000000000000e+00
  5  0.00000000000000000e+00
  6  1.77146011704106335e-03
  7  0.00000000000000000e+00
  8  0.00000000000000000e+00
  9 -4.80000097346545279e-04
 10  1.39013834758571625e-05
 11  0.00000000000000000e+00
 12  0.00000000000000000e+00
 13 -1.39822026074492775e-04
 14  0.00000000000000000e+00
 15 -2.48316020753471748e-04
 16  4.20097131947155702e-04
 17  0.00000000000000000e+00
 18 -5.74904249836309948e-04
 19  4.87036596243491202e-04
 20 -2.48316020753471748e-04
 21  4.20097131947155702e-04
 22  0.00000000000000000e+00
 23  5.74904249836309948e-04
 24  4.87036596243491202e-04
 Sym= A1
 Ene= -1.33767141221376074e+00
 Spin= Alpha
 Occup=  2.00000000000000000e+00
  1  8.23750319287331126e-03
  2 -4.42693483105670693e-01
  3 -3.74498035531203644e-01
  4  0.00000000000000000e+00
  5  0.00000000000000000e+00
  6 -7.94258623925411383e-02
  7  0.00000000000000000e+00
  8  0.00000000000000000e+00
  9  1.34092445806940908e-02
 10 -1.29606223143065302e-03
 11  0.00000000000000000e+00
 12  0.00000000000000000e+00
 13  2.99830598541997798e-03
 14  0.00000000000000000e+00
 15 -1.94341522346533402e-01
 16 -1.00103187758395738e-02
 17  0.00000000000000000e+00
 18 -3.73152124438631272e-02
 19  2.06694627945085245e-02
 20 -1.94341522346533402e-01
 21 -1.00103187758395738e-02
 22  0.00000000000000000e+00
 23  3.73152124438631272e-02
 24  2.06694627945085245e-02

文件 2:

[Molden Format]
[Molpro variables]
_NUMVAR=  897.0000000000000    
_AVOGAD= 0.6022136736000000E+24
_BOLTZ= 0.1380658000000000E-22
_CLIGHT=  29979245800.00000    
_EV= 0.3674932379085202E-01
_ENULL= 0.8854187817000000E-11
_KJOULE= 0.3808798324128737E-03
_KJOULE/MOL= 0.3808798324128737E-03
_CM= 0.4556335267302901E-05
_CM-1= 0.4556335267302901E-05
_DEB2SI= 0.3335641000000000E-29
_AMU2AU=  1822.888390000000    
_KCAL= 0.1593601117815568E-02
_KCAL/MOL= 0.1593601117815568E-02
_KELVIN= 0.3166811411060229E-05
_JOULE= 0.4359748200000000E-17
_HERTZ= 0.1519829846004524E-15
_HZ= 0.1519829846004524E-15
_JPCAL=  4.184000000000000    
_PLANCK= 0.6626075540000000E-33
_TOA= 0.5291772109200000    
_TOANG= 0.5291772109200000    
_TORAD= 0.1745329251994330E-01
_TOCM=  219474.6306700000    
_TODEBYE=  2.541580000000000    
_TOE= 0.4803242000000000E-09
_TOEV=  27.21138505000000    
_TOHERTZ=  6579683920729000.    
_TOHZ=  6579683920729000.    
_TOK=  315775.0400000000    
_TOKCAL=  627.5096000000000    
_TOKELVIN=  315775.0400000000    
_TOKJ=  2625.500000000000    
_TOKJOULE=  2625.500000000000    
_TOKG= 0.1660565586000000E-26
_ANG=  1.889726124565062    
_ANGSTROM=  1.889726124565062    
_RPAREF=  0.000000000000000    
_RPADISS=  0.000000000000000    
_IGNORE_UNDEF=  2.000000000000000    
_STRICTCHECK=  0.000000000000000    
_SEW_ONEEL=  0.000000000000000    
_CPPDONE=FALSE
_DKROLL_DONE=  0.000000000000000    
_FOCKDONE=TRUE
_GRADONE=FALSE
_HESSDONE=FALSE
_INTDONE=TRUE
_SCFDONE=TRUE
_MODUL_BLOCK=FALSE
_MODUL_CUDA=FALSE
_MODUL_SLATER=FALSE
_MODUL_MRCC=FALSE
_MODUL_NBO6=FALSE
_MODUL_NECI=TRUE
_MODUL_AIMS=TRUE
_MRCC_EXEC=
_FITC_AO_SAVED=FALSE
_FITC_MO_SAVED=FALSE
_SHARED_FS=TRUE
_LSTYP=SEWARD
_INTYP=INTS
_GEOMTYP=ZMAT
_GRADTYP=ALASKA
_DIRECT=FALSE
_DKHO= -1.000000000000000    
_SYNCM=  0.000000000000000    
_BASIS=AUG-CC-PVTZ
_ZORIENT=  0.000000000000000    
_NELEC=  10.00000000000000    
!SPIN=  0.000000000000000    
_QSDSTEP=  0.000000000000000    
_QSDIRC=  0.000000000000000    
_PROGRAM=RHF-SCF
_CPUSTEP= 0.2000000000000002    
_SYSSTEP= 0.4000000000000004E-01
_WALLSTEP= 0.3900000000000000    
!DFSCF=  0.000000000000000    
!SCSGRD=  0.000000000000000    
_BASINP=  610.0000000000000    
_CPUTOT=  1.600000000000000    
_CPUSORT= 0.2100000000000000    
_SYSSORT= 0.3000000000000003E-01
_WALLSORT= 0.3200000002980232    
_DFTEXFAC=  1.000000000000000    
_DMX=  0.000000000000000    
_DMY=  0.000000000000000    
_DMZ= 0.7630238919693926    
_DMX_SCF=  0.000000000000000    
_DMY_SCF=  0.000000000000000    
_DMZ_SCF= 0.7630238919693926    
_EHOMO=-0.5112368090014537    
_ELUMO= 0.2975373354087893E-01
_EIGS= -20.56350049329804    
_ENERGY= -76.06120311938969    
_ENERGY_METHOD=RHF
_ENERGY_BASIS=aug-cc-pVTZ
_GEOMETRY_METHOD=
_GEOMETRY_BASIS=
_ENUC=  9.348802247698661    
_GRADEXFAC=  9999.000000000000    
_GRADMETHOD=  1.000000000000000    
_NMRMETHOD=  0.000000000000000    
_NMRMAGNET=  0.000000000000000    
!GRID_FREEZE= -1.000000000000000    
_ITERATIONS=  9.000000000000000    
_LASTNELEC=  10.00000000000000    
_LASTSPIN=  0.000000000000000    
_LASTSYM=  1.000000000000000    
_MPP_PROG=  1.000000000000000    
_MSNSTATES=  0.000000000000000    
_RS2PHASE=  0.000000000000000    
_NPROC_MPP=  23.00000000000000    
_NPROC_MPPX=  23.00000000000000    
_NPROC_SMP=  1.000000000000000    
_NUMSTEP=  0.000000000000000    
_ORBITAL=  2100.200000000000    
_PI=  3.141592653589793    
_SEIG_MIN= 0.3697027323475132E-03
_SEWPROP=TRUE
_STATUS=  1.000000000000000    
_SYSTOT= 0.1900000000000001    
_VERSION=  2015001.000000000    
_WALLTOT=  3.040000000000000    
_DATE=11-Apr-20
_LASTORB=RHF
_MACHINE= 64 bit mpp version
_OUTPUT=/global/cscratch1/sd/rbello/Quantum_Chem/molpro.xml
_PGROUP=C2v
_TIME=13:36:56
_LIBMOL=/global/common/cori/software/molpro/2015/molprop_2015_1_linux_x86_64_i8/lib/
_VIGRDC=  0.000000000000000    
!VIBCNT=  0.000000000000000    
!RGHFCNT=  0.000000000000000    
!SURFREC=  0.000000000000000    
!ROTREC=  0.000000000000000    
!POLYREC=  0.000000000000000    
!DIPREC=  0.000000000000000    
!DIP4DREC=  0.000000000000000    
!DIPROT=  0.000000000000000    
!DIP4DROT=  0.000000000000000    
!PTREC=  0.000000000000000    
!PT4DREC=  0.000000000000000    
!VSCFREC=  0.000000000000000    
!VCIREC=  0.000000000000000    
!PVCIREC=  0.000000000000000    
!VMP2REC=  0.000000000000000    
!VMCSCFREC=  0.000000000000000    
!VMRCIREC=  0.000000000000000    
!VIBSTATEREC=  0.000000000000000    
!VIBSTATECNT=  0.000000000000000    
!VIBINFO=  0.000000000000000    
!GAREC=  0.000000000000000    
!SCFORB=  21002.00000000000    
!NEWMOL= -1.000000000000000    
!NEWSYM= -1.000000000000000    
_SLFILES=slscratch
_NGSSTO=  9.000000000000000    
_SIN=SIN
_COS=COS
_TAN=TAN
_ACOS=ACOS
_ASIN=ASIN
_ATAN=ATAN
_COSH=COSH
_SINH=SINH
_TANH=TANH
_ABS=ABS
_MOD=MOD
_ERF=ERF
_REFC=REFC
_ERFC=ERFC
_HYPOT=HYPOT
_J0=J0
_J1=J1
_JN=JN
_GAMMA=GAMMA
_LGAMMA=LGAMMA
_Y0=Y0
_Y1=Y1
_YN=YN
_EXP=EXP
_LOG=LOG
_LOG10=LOG10
_SQRT=SQRT
_INT=INT
_NINT=NINT
_MAX=MAX
_MIN=MIN
_COSMO=  0.000000000000000    
_COSMOIT=  0.000000000000000    
_COSMOES=  0.000000000000000    
_COSMOEDS=  0.000000000000000    
_COSMOEC=  0.000000000000000    
_COSMOEDCORR=  0.000000000000000    
_FEPSI=  1.000000000000000    
_SL_IsMM=  0.000000000000000    
_SL_NSYM=  4.000000000000000    
_SL_System_BitSw=  0.000000000000000    
_SL_Unique_atoms=  0.000000000000000    
_SL_GRAD=  0.000000000000000    
_SL_MAXUPD_Hess=  5.000000000000000    
_SL_N_PRINT_COD=  5.000000000000000    
_SL_PRINT_CODES=  0.000000000000000    
_SL_BasType=  0.000000000000000    
_SL_Energy_Pred=  0.000000000000000    
_SL_New_Coords=  0.000000000000000    
_SL_Total_Nuclea=  0.000000000000000    
_SL_Highest_Mltp=  0.000000000000000    
_SL_Ref_Geom=  0.000000000000000    
_SL_Symmetry_ope=  1.000000000000000    
_SL_agrad=  0.000000000000000    
_SL_GrdMax=  0.000000000000000    
_SL_GrdNorm=  0.000000000000000    
_SL_GrdRMS=  0.000000000000000    
_SL_Hss_Q=  0.000000000000000    
_SL_Hss_X=  0.000000000000000    
_SL_BMxOld=  0.000000000000000    
_SL_KtB=  0.000000000000000    
_SL_TROld=  0.000000000000000    
_SL_No_of_Intern=  0.000000000000000    
_SL_Hess=  0.000000000000000    
_SL_K=  0.000000000000000    
!LATTICE= -1.000000000000000    
!NEWLATTICE=  0.000000000000000    
!LATGRAD_VAR=FALSE
!SEW_LATTICE=  1.000000000000000    
!LOCAL=  0.000000000000000    
!SAVEDOM= -1.000000000000000    
!RESTDOM= -1.000000000000000    
!SAVEDOM_HF= -1.000000000000000    
!RESTDOM_HF= -1.000000000000000    
!HEs-s-rEC=  0.000000000000000    
!FREQREC=  0.000000000000000    
!NMODREC=  0.000000000000000    
!GTASKS=  0.000000000000000    
!HTASKS=  0.000000000000000    
!SAVEDOM_EOM= -1.000000000000000    
!RESTDOM_EOM= -1.000000000000000    
!GRADSTATE= -1.000000000000000    
!THRCHG= 0.1000000000000000    
!DEBUG= -1.000000000000000    
!CFIT_CPHF=  0.000000000000000    
!RDOMAUX_CPHF=  3.000000000000000    
!THRAO_CPHF= 0.1000000000000000E-09
!THRMO_CPHF= 0.1000000000000000E-08
!THROV_CPHF= 0.1000000000000000E-11
!THRSW_CPHF= 0.1000000000000000E-09
!THRPROD_CPHF= 0.1000000000000000E-08
!THRAOPR_CPHF= 0.1000000000000000E-11
!CFIT_SCFGRD=  0.000000000000000    
!RDOMAUX_SCFGRD=  5.000000000000000    
!THRAO_SCFGRD= 0.1000000000000000E-09
!THRMO_SCFGRD= 0.1000000000000000E-08
!THROV_SCFGRD= 0.1000000000000000E-11
!THRSW_SCFGRD= 0.1000000000000000E-09
!THRPROD_SCFGRD= 0.1000000000000000E-08
!THRAOPR_SCFGRD= 0.1000000000000000E-11
!LOCFIT_SCF=  0.000000000000000    
!LOCFIT_MP2= -1.000000000000000    
_RIDOM=  0.000000000000000    
!CC2_METHOD=  0.000000000000000    
_BASIS2006=FALSE
_SVDDEL= -1.000000000000000    
_SVDDELA= -1.000000000000000    
_SVDDELP= -1.000000000000000    
_SVDDELF= -1.000000000000000    
_SVDDELS= -1.000000000000000    
_SVDDEL_ABS= -1.000000000000000    
_SVDDELA_ABS= -1.000000000000000    
_SVDDELP_ABS= -1.000000000000000    
_SVDDELF_ABS= -1.000000000000000    
_SVDDELS_ABS= -1.000000000000000    
_MAXBASIS=  0.000000000000000    
!OPT_CONICAL=  0.000000000000000    
!CLEARSP_JGRID=  0.000000000000000    
_GRDHYBR=  0.000000000000000    
_STATEGRAD=  1.000000000000000    
_4EXT_NDISKS=  1.000000000000000    
!ZMP=  0.000000000000000    
_MAXIT_ZMP=  500.0000000000000    
_MAXDIS_ZMP=  100.0000000000000    
_ACCU_ZMP=  14.00000000000000    
_SHIFTA_ZMP= -100.0000000000000    
!EMBEDFLAG=  1.000000000000000    
!EMBED=FALSE
!EMBED_F12=FALSE
!EMBEDNUC=  0.000000000000000    
_EMBEDENERGY=  0.000000000000000    
_KSPACETIME=  0.000000000000000    
_RSPACETIME=  0.000000000000000    
!EMBED_DFC=FALSE
!NUM_VOIDS=  0.000000000000000    
_IBODY=  0.000000000000000    
_EMBE_TOTAL=  0.000000000000000    
_EMBE_HF=  0.000000000000000    
_EMBE_CORR=  0.000000000000000    
!EMBED_PROJ=FALSE
!EMBED_TRUNC=  0.000000000000000    
!EMB_PROJ_ZMU=  1000000.000000000    
!CORRECTION=  0.000000000000000    
!CORRECTION_HF=  0.000000000000000    
!EMBED_ENERGY=  0.000000000000000    
!EMBED_NAXC=FALSE
!EMBED_AORB=  0.000000000000000    
!EMBED_NACORR=  0.000000000000000    
!EMBED_MP2=FALSE
!EMBED_TRUNC_PERTURBATION=FALSE
!EMBED_FROZEN=  0.000000000000000    
_CHANGE_BASIS=FALSE
_RESTORE_BASIS=FALSE
_FIX_EMBED_GUESS=FALSE
!EDFT=  0.000000000000000    
_IFASTLATGRAD=  0.000000000000000    
_SINDEX=  0.000000000000000    
_AVDOM_OSV=  0.000000000000000    
_AVDOM_PNO=  0.000000000000000    
_AVDOM_PAO=  0.000000000000000    
_AVDOM_LMO=  0.000000000000000    
_CC2OSV_FLAG=  0.000000000000000    
_TMADC2_FLAG=FALSE
_PROPADC2_FLAG=FALSE
_LEN_VirQ=  0.000000000000000    
_GRDCC2LR_FLAG=FALSE
_GRDADC2_FLAG=FALSE
R=  0.000000000000000    
AHOH=  104.4776000000000    
GX1=  0.000000000000000    
GY1=  0.000000000000000    
GZ1=  0.000000000000000    
GX2=  0.000000000000000    
GY2= 0.7530886081000000    
GZ2= 0.5642560353000000    
GX3=  0.000000000000000    
GY3=-0.7530886081000000    
GZ3= 0.5642560353000000    
ACCU=  13.00000000000000    
NITORT=  1.000000000000000    
[Atoms] Angs
O     1    8         0.0000000000        0.0000000000        0.0000000000
H     2    1         0.0000000000        0.7530886081        0.5642560353
H     3    1         0.0000000000       -0.7530886081        0.5642560353
[GTO]
   1 0
 s   10  1.00
  0.1533000000D+05  0.5079996682D-03
  0.2299000000D+04  0.3928997434D-02
  0.5224000000D+03  0.2024298678D-01
  0.1473000000D+03  0.7918094828D-01
  0.4755000000D+02  0.2306868493D+00
  0.1676000000D+02  0.4331177171D+00
  0.6207000000D+01  0.3502597712D+00
  0.1752000000D+01  0.4272797209D-01
  0.6882000000D+00 -0.8153994674D-02
  0.2384000000D+00  0.2380998445D-02
 s   10  1.00
  0.1533000000D+05 -0.1150000604D-03
  0.2299000000D+04 -0.8950004697D-03
  0.5224000000D+03 -0.4636002433D-02
  0.1473000000D+03 -0.1872400983D-01
  0.4755000000D+02 -0.5846303068D-01
  0.1676000000D+02 -0.1364630716D+00
  0.6207000000D+01 -0.1757400922D+00
  0.1752000000D+01  0.1609340845D+00
  0.6882000000D+00  0.6034183167D+00
  0.2384000000D+00  0.3787651988D+00
 s   10  1.00
  0.1533000000D+05  0.0000000000D+00
  0.2299000000D+04  0.0000000000D+00
  0.5224000000D+03  0.0000000000D+00
  0.1473000000D+03  0.0000000000D+00
  0.4755000000D+02  0.0000000000D+00
  0.1676000000D+02  0.0000000000D+00
  0.6207000000D+01  0.0000000000D+00
  0.1752000000D+01  0.1000000000D+01
  0.6882000000D+00  0.0000000000D+00
  0.2384000000D+00  0.0000000000D+00
 s   10  1.00
  0.1533000000D+05  0.0000000000D+00
  0.2299000000D+04  0.0000000000D+00
  0.5224000000D+03  0.0000000000D+00
  0.1473000000D+03  0.0000000000D+00
  0.4755000000D+02  0.0000000000D+00
  0.1676000000D+02  0.0000000000D+00
  0.6207000000D+01  0.0000000000D+00
  0.1752000000D+01  0.0000000000D+00
  0.6882000000D+00  0.0000000000D+00
  0.2384000000D+00  0.1000000000D+01
 s    1  1.00
  0.7376000000D-01  0.1000000000D+01
 p    5  1.00
  0.3446000000D+02  0.1592799074D-01
  0.7749000000D+01  0.9973994203D-01
  0.2280000000D+01  0.3104918195D+00
  0.7156000000D+00  0.4910257146D+00
  0.2140000000D+00  0.3363368045D+00
 p    5  1.00
  0.3446000000D+02  0.0000000000D+00
  0.7749000000D+01  0.0000000000D+00
  0.2280000000D+01  0.0000000000D+00
  0.7156000000D+00  0.1000000000D+01
  0.2140000000D+00  0.0000000000D+00
 p    5  1.00
  0.3446000000D+02  0.0000000000D+00
  0.7749000000D+01  0.0000000000D+00
  0.2280000000D+01  0.0000000000D+00
  0.7156000000D+00  0.0000000000D+00
  0.2140000000D+00  0.1000000000D+01
 p    1  1.00
  0.5974000000D-01  0.1000000000D+01
 d    1  1.00
  0.2314000000D+01  0.1000000000D+01
 d    1  1.00
  0.6450000000D+00  0.1000000000D+01
 d    1  1.00
  0.2140000000D+00  0.1000000000D+01
 f    1  1.00
  0.1428000000D+01  0.1000000000D+01
 f    1  1.00
  0.5000000000D+00  0.1000000000D+01
  
   2 0
 s    5  1.00
  0.3387000000D+02  0.6067999217D-02
  0.5095000000D+01  0.4530799416D-01
  0.1159000000D+01  0.2028219738D+00
  0.3258000000D+00  0.5039029350D+00
  0.1027000000D+00  0.3834209505D+00
 s    5  1.00
  0.3387000000D+02  0.0000000000D+00
  0.5095000000D+01  0.0000000000D+00
  0.1159000000D+01  0.0000000000D+00
  0.3258000000D+00  0.1000000000D+01
  0.1027000000D+00  0.0000000000D+00
 s    5  1.00
  0.3387000000D+02  0.0000000000D+00
  0.5095000000D+01  0.0000000000D+00
  0.1159000000D+01  0.0000000000D+00
  0.3258000000D+00  0.0000000000D+00
  0.1027000000D+00  0.1000000000D+01
 s    1  1.00
  0.2526000000D-01  0.1000000000D+01
 p    1  1.00
  0.1407000000D+01  0.1000000000D+01
 p    1  1.00
  0.3880000000D+00  0.1000000000D+01
 p    1  1.00
  0.1020000000D+00  0.1000000000D+01
 d    1  1.00
  0.1057000000D+01  0.1000000000D+01
 d    1  1.00
  0.2470000000D+00  0.1000000000D+01
  
   3 0
 s    5  1.00
  0.3387000000D+02  0.6067999217D-02
  0.5095000000D+01  0.4530799416D-01
  0.1159000000D+01  0.2028219738D+00
  0.3258000000D+00  0.5039029350D+00
  0.1027000000D+00  0.3834209505D+00
 s    5  1.00
  0.3387000000D+02  0.0000000000D+00
  0.5095000000D+01  0.0000000000D+00
  0.1159000000D+01  0.0000000000D+00
  0.3258000000D+00  0.1000000000D+01
  0.1027000000D+00  0.0000000000D+00
 s    5  1.00
  0.3387000000D+02  0.0000000000D+00
  0.5095000000D+01  0.0000000000D+00
  0.1159000000D+01  0.0000000000D+00
  0.3258000000D+00  0.0000000000D+00
  0.1027000000D+00  0.1000000000D+01
 s    1  1.00
  0.2526000000D-01  0.1000000000D+01
 p    1  1.00
  0.1407000000D+01  0.1000000000D+01
 p    1  1.00
  0.3880000000D+00  0.1000000000D+01
 p    1  1.00
  0.1020000000D+00  0.1000000000D+01
 d    1  1.00
  0.1057000000D+01  0.1000000000D+01
 d    1  1.00
  0.2470000000D+00  0.1000000000D+01
 
 
[MO]
 Sym=      1.1
 Ene=     -20.5635
 Spin= Alpha
 Occup=    2.000000
1 0.99886977633685
2 -0.002215397472811
3 0.001339812894581
4 -0.000004180768445
5 0.000548838632543
6 0.0
7 0.0
8 0.004778581946589
9 0.0
10 0.0
11 -0.003730531833399
12 0.0
13 0.0
14 -0.001157169841262
15 0.0
16 0.0
17 0.000185654641842
18 -0.000090895245319
19 0.000082587393316
20 0.000008307852004
21 0.0
22 0.0
23 0.0
24 0.000444705577625
25 -0.000386073005962
26 -0.000058632571663
27 0.0
28 0.0
29 0.0
30 -0.000178445813651
31 0.000109582826994
32 0.000068862986658
33 0.0
34 0.0
35 0.0
36 0.0
37 0.0
38 -0.000039290663317
39 0.0
40 0.0
41 -0.000009119140879
42 0.0
43 0.0
44 0.000061833097313
45 0.0
46 0.0
47 0.0
48 0.000101391442167
49 0.0
50 0.0
51 0.000087807533700
52 0.0
53 0.0
54 -0.000223838427914
55 0.0
56 0.002239240169404
57 -0.000535094559902
58 -0.001276546316515
59 0.000012380675065
60 0.0
61 -0.000102597933280
62 -0.000055538548035
63 0.0
64 -0.000530661889263
65 -0.000462529931180
66 0.0
67 -0.000076124646814
68 -0.000038598432129
69 0.000102705643914
70 -0.000063087655025
71 -0.000039617988889
72 0.0
73 0.0
74 -0.000189205570701
75 -0.000216447626108
76 0.000193585960579
77 0.000022861665528
78 0.0
79 0.0
80 0.000379059881317
81 0.002239240169404
82 -0.000535094559902
83 -0.001276546316515
84 0.000012380675065
85 0.0
86 0.000102597933280
87 -0.000055538548035
88 0.0
89 0.000530661889263
90 -0.000462529931180
91 0.0
92 0.000076124646814
93 -0.000038598432129
94 0.000102705643914
95 -0.000063087655025
96 -0.000039617988889
97 0.0
98 0.0
99 0.000189205570701
100 -0.000216447626108
101 0.000193585960579
102 0.000022861665528
103 0.0
104 0.0
105 -0.000379059881317


主要区别在于文件 2 有 [Molpro variables] 部分。

我对文件 1 的输出(注意:预期/期望的输出):

O    1    8         0.000000000000       0.000000000000      -0.124302651265
H    2    1         0.000000000000      -1.424704710000       0.986386639735
H    3    1        -0.000000000000       1.424704710000       0.986386639735

我对文件 2 的输出:


换句话说,它是空白的。

但是,如果我将要查找的字符串从 line.fing("Atoms") 更改为 line.find("toms") 这是我的输出

文件 1:

O    1    8         0.000000000000       0.000000000000      -0.124302651265
H    2    1         0.000000000000      -1.424704710000       0.986386639735
H    3    1        -0.000000000000       1.424704710000       0.986386639735

文件 2:

Atoms] Angs
O    1    8         0.000000000000       0.000000000000      -0.124302651265
H    2    1         0.000000000000      -1.424704710000       0.986386639735
H    3    1        -0.000000000000       1.424704710000       0.986386639735

【问题讨论】:

【参考方案1】:

这里有个问题:

if (line.find("Molpro") != std::string::npos) 
    std::getline(source, line, '['); // reads until [

if (line.find("Atoms") != std::string::npos) 

file2 中,一旦您读取Molpro,您将提取所有内容直到下一个[,并覆盖line。在while 循环的当前迭代中,搜索Atoms 失败(应该如此),但随后您检查文件中确实存在的字符串MO(并导致文件中剩余的所有内容被跳过)。这会导致您也跳过您正在寻找的Atoms 字符串。

只需添加continue,如下所示:

if (line.find("Molpro") != std::string::npos) 
    std::getline(source, line, '['); // reads until [
    continue; // <-- here

if (line.find("Atoms") != std::string::npos) 

在我的机器上运行 ./a.out file2 会生成输出:

O     1    8         0.0000000000        0.0000000000        0.0000000000
H     2    1         0.0000000000        0.7530886081        0.5642560353
H     3    1         0.0000000000       -0.7530886081        0.5642560353

这似乎是正确的。

坦率地说,我不知道为什么执行find(toms) 会生成它的输出;我的猜测是它与 file2 中出现的几十个 MO 有关。反正bug的影响不是太重要,一旦发现bug。

我建议考虑一下您的代码的结构。看来您可能期望分支是互斥的,在这种情况下,您应该为要查找的每个字符串嵌套 if-else 语句。

【讨论】:

啊,我想我明白了。在 if 语句中为 getline(source,tmp_line,'[') 创建一个临时字符串变量会给我想要的结果吗?还使用该 tmp_line 字符串追加... 是的,应该也可以。但我仍然建议为AtomsGTOMO 嵌套if-else 分支。我认为这就是你想要的 :) 编写代码来做你想做的事情,而不是仅仅因为你发现一个错误而编辑代码:) 很抱歉,我对你所说的 if-else 声明的逻辑感到困惑。 else 分支会是什么?我宁愿使用其他答案代码,但它将整个字符串放在一行上,并且不会打印出上面所需的输出。 你的代码有if (Molpro) if (Atoms) if (GTO) if (MO) 。我建议if (Molpro) else if (Atoms) else if (GTO) else if (MO) 啊,对了!一旦你以某种方式思考,那就太棒了,很难看出这种想法有多模糊。这更清楚,我想要做什么。【参考方案2】:

让我们一步一步来。

while (std::getline(source, line))  

    // Just read the line: 
    // [Molpro variables]
    //
    //  So line contains the string: "[Molpro variables]"

    if (line.find("Molpro") != std::string::npos) 
       // this is finds the word:  Molpro
       // So we enter the if statement.

       std::getline(source, line, '['); 
       // This reads all the way to the next '['
       // Which is the lines ending with:
       //        NITORT=  1.000000000000000    
       //        [  => Note this character is discarded.
       //              It is not on the stream nor in the variable line
       // Thus the next line of characters left on the stream are:
       //        Atoms] Angs
       //
       // But the main thing to remember is that you have a lot of text
       // inside the variable `line` (about 339 lines).
    

    if(line.find("Atoms") != std::string::npos) 

       // We don't find the word "Atoms" in the 339 lines.
       // Though if you look you will see atoms in those 339
       // So this is the case where this if statement is not entered.
       // So the next two lines do not get executed.
        std::getline(source, line, '['); // reads until [
        ParticlesString.append(line);
    


    if(line.find("GTO") != std::string::npos) 
        // GTO is not in the 339 lines.
        // So this code is not executed.
        std::getline(source, line, '['); // reads until [
        GaussianString.append(line); 
    
    if(line.find("MO") != std::string::npos) 

        // You will find the string "MO" in the 339 lines read above.
        // So the following code will be executed.

        std::getline(source, line, '\0'); // reads until end of file
        // You have read the data from >Atoms] Ang all the way to the
        // end of the file.
      
        MOString.append(line);
    
 
 // The loop is not re-executed.
 // As you read the whole file.

坦率地说,你的设计有缺陷:

您应该设置一个状态机来跟踪您所在文件的哪个部分。不要尝试向前跳过,只需等待正确的节标题出现然后设置节。

void readFileToStrings(const std::string& filename,
                         std::string& ParticlesString,
                         std::string& GaussianString, 
                         std::string& MOString) 
    std::ifstream file(filename);
    std::string section = "None";

    std::string line;
    // Only read lines in one place
    // This is your control for processing the file
    while(std::getline(file, line)) 

       // Check to ses if you are entering a special section of
       // the data file. It seems like each section starts with
       // [XXXX] So check for the ones that are important to you.
       if (line.substr(0, 7) == "[Atoms]") 
           section = "Atoms";
       else if (line.substr(0, 5) == "[GTO]") 
           section = "GTO";
       else if (line.substr(0, 4) == "[MO]") 
           section = "MO";
       else if (line[0] == '[') 
           // Make sure you notice when you move to a section you
           // don't care about.
           section = "None";
       else 
           // We are reading a data line:
           // So for each data line append it to the appropriate
           // work items.
           if (section == "Atoms") 
               ParticlesString.append(line);
           
           else if (section == "GTO") 
               GaussianString.append(line);
           
           else if (section == "MO") 
               MOString.append(line);
           
       
   

【讨论】:

刚刚复制并粘贴了您的代码,而您的 else if 语句中缺少右括号。这段代码的输出也给了我不同的格式,它破坏了一切...... 通过“不同的格式”我的意思是所有的输出都在一行...

以上是关于用 find 解析文件会给出不同文件的奇怪结果的主要内容,如果未能解决你的问题,请参考以下文章

C 文件与 C++ 文件的 Eclipse CDT 索引器结果不同

`std::find()` 用字符串产生奇怪的结果

使用 Node/Express 解析 CSV 文件会吐出奇怪的 \x001 代码

使用Java解析具有奇怪扩展类型的文件[关闭]

脚本在解析链接时给出重复的结果

Coldfusion - 奇怪的解析时间结果