我正在尝试从这样的文件中读取
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/