gpt4 book ai didi

c++ - 读取包含数据的文件

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:53 24 4
gpt4 key购买 nike

我是 C++ 编程新手。我正在尝试读取文件中的数据,其内容如下:

AS G02  2009 01 30 00 00  0.000000  2    1.593749310156e-04  4.717165038980e-11
AS G03 2009 01 30 00 00 0.000000 2 3.458468649886e-04 4.542246790350e-11
AS G04 2009 01 30 00 00 0.000000 2 -3.176765824224e-04 2.733827659950e-11
AS G05 2009 01 30 00 00 0.000000 2 -6.126657874204e-04 3.269050090460e-11

然后我会将此数据写入输出文件以供稍后处理。输出应该是这样的:

02  2009 01 30 00 00  0.000000  2    1.593749310156e-04  4.717165038980e-11
03 2009 01 30 00 00 0.000000 2 3.458468649886e-04 4.542246790350e-11
04 2009 01 30 00 00 0.000000 2 -3.176765824224e-04 2.733827659950e-11
05 2009 01 30 00 00 0.000000 2 -6.126657874204e-04 3.269050090460e-11

谁能帮忙。问候

最佳答案

假设您需要在 C++ 中执行此操作(awk 会更容易),那么您需要了解 iostream。

#include <iostream>
#include <sstream>
#include <fstream>

int main()
{
std::ifstream input("file.txt");
std::stringstream sstr;
std::string line;

while(getline(input,line)) {
if (line.length() > 4) {
std::cout << line.substr(4); // Print from the 4th character to the end.
}
}
}

默认情况下,getline 读取输入直到它到达行尾。您还可以让它读取输入,直到它获得特定字符,例如空格或逗号与 getline(stream,string,delimiter)。通过这种方式,您可以一次读取一行一个单词并处理各个值。

附言。 SO 什么时候会获得 intellisense?

关于c++ - 读取包含数据的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1208935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com