c++ - 如何在c++中从具有不同行大小的文件中读取行?

标签 c++ file readfile

我正在尝试从这样的文件中读取

2                   #number of process

1                   #process name
0 1000              #start and finsh time of process
1 400               #number of memmory #memmory size

2                   #process name
0 2000              #start and finsh time of process
2 200 400           #number of memmory #memmory size

进程数和内存数可以不同
我的主要问题是如何从具有不同内存数量的文件中读取数据?

最佳答案

假设输入文件格式正确且不包含任何错误,则可以按原样读取数字,然后使用循环读取变量金额记录或值。

也许是这样的:

int process_count;
file >> process_count;

for (unsigned p = 0; p < process_count; ++p)
{
    int process_name, start_time, finish_time, memory_count;
    std::vector<int> memory;

    file >> process_name >> start_time >> finish_time >> memory_count;

    for (unsigned m = 0; m < memory_count; ++m)
    {
        int memory_size;
        file >> memory_size;
        memory.push_back(memory_size);
    }

    // Here all data for the "process" have been read from the file, use it...
}

关于c++ - 如何在c++中从具有不同行大小的文件中读取行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59386959/

相关文章:

c# - 使用c#从文档中获取纯文本

c++ - 访问呈现的 WPF 页面/窗口的底层 DirectX 表面/渲染目标

c++ - 在 win32 API(无 MFC)中的工具栏中获取渐变的正确方法是什么?

c++ - TRACKER : error TRK0005: Failed to locate: "xsd.exe". 系统找不到指定的文件

c# - 从 Amazon s3 流中读取文本

python - 读取python文件并使用输入

c++ - `auto const& x `在C++中做什么?

python - 仅在 CSV 文件中查找第一个匹配项

java - 删除文本文件中的多余行

javascript - AngularJs ng-click 不适用于文件上传字段