如何从c ++中的文件中提取数据?

时间:2015-07-04 11:52:16

标签: c++ file

我有一个由“已用时间”信息组成的文件:

"D:\p\prototypes\performance\target_win64-vc10sp1\CMakeFiles\3.0.20140226\CompilerIdC\CompilerIdC.vcxproj" (default target) (1) ->
(DoLinkOutputFilesMatch target) -> 
      C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(990,5): warning MSB8012: TargetPath(D:\projekte\.\CompilerIdC.exe) does not match the Linker's OutputFile property value (D:\p\\prototypes\performance\target_win64-vc10sp1\CMakeFiles\3.0.20140226\CompilerIdC\CompilerIdC.exe). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile). [D:\p\prototypes\jperformance\target_win64-vc10sp1\CMakeFiles\3.0.20140226\CompilerIdC\CompilerIdC.vcxproj]

        1 Warning(s)
        0 Error(s)

    Time Elapsed 00:00:01.50
Build succeeded.

"D:\p\prototypes\performance\target_win64-vc10sp1\CMakeFiles\3.0.20140226\CompilerIdCXX\CompilerIdCXX.vcxproj" (default target) (1) ->
(DoLinkOutputFilesMatch target) -> 
  C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(990,5): warning MSB8012: TargetPath(D:\projekte\.\CompilerIdCXX.exe) does not match the Linker's OutputFile property value (D:\p\CD_8570w\robotic_applications\prototypes\jenkins_performance\target_win64-vc10sp1\CMakeFiles\3.0.20140226\CompilerIdCXX\CompilerIdCXX.exe). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile). [D:\p\CD_8570w\robotic_applications\prototypes\jenkins_performance\target_win64-vc10sp1\CMakeFiles\3.0.20140226\CompilerIdCXX\CompilerIdCXX.vcxproj]

    1 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.54
Build succeeded.

    0 Warning(s)

    0 Error(s)

Time Elapsed 00:00:02.06

现在我想使用c ++程序仅从该文件中提取最后一个“Time逝去”数据,即“02.06”。目前我正在做的事情如下:

   std::string line;
   std::string token = "Time Elapsed ";
   std::ifstream file(my_file_path);
   while(getline(file, line)){
       if (line.find( token)!= std::string::npos) {
             // do some stuff
        }
     }

发生的事情是我获得所有“Time elapsed”值,但我只需要文件末尾的那个。所以,请告诉我如何才能获得这个价值。

2 个答案:

答案 0 :(得分:1)

您可以将您找到的值存储起来并使用if (line.find(token)处理在while语句之外的变量中。当while语句结束时(因此你处理整个输入),你只会在该变量中得到最后一个结果。

答案 1 :(得分:1)

作为@cookiesoft回答的补充:

std::string line;
std::string token = "Time Elapsed ";
std::ifstream file(my_file_path);

std::string lastValue; // << Add this

while(getline(file, line)){
    if (line.find( token)!= std::string::npos) {
        // do some stuff

        lastValue = your_computed_value; // << Add this
    }
}

// Now lastValue contains the last processed value