gpt4 book ai didi

c++ - 如何使用 strip 化算法读取 residence.dat 文件 C++

转载 作者:行者123 更新时间:2023-11-28 07:18:35 37 4
gpt4 key购买 nike

我必须从两个 .dat 文件中读取数据,但是我的控制台输出函数有问题,而且我的 C++ 技能仍然生疏,有人可以帮忙吗?

Read a subset of the address records from residences.dat using striping. For n >processes, each process evaluates a unique subset of records based on every nth record. The number of records in this subset should be approximately #-of-residence-records /#-of-processes. Across all the parallel processes used no address should be omitted and >none should be processed more than once. Also note that only ONE record at a time should be stored in memory by any process; don’t read the entire dataset into a data structure in memory since this is entirely unnecessary and consumes too much RAM!

数据示例

329267.349 4847214.382
318141.019 4851350.892
319526.06 4850474.347
322666.48 4840244.995
316578.529 4837299.827
320090.607 4840439.088



//read file and populate the vectors


ifstream foodbankFile("/Users/abdallaelnajjar/Documents/XCodeProjects/cpp_projects/MPI_Project2/foodbanks.dat");
ifstream residenceFile("/Users/abdallaelnajjar/Documents/XCodeProjects/cpp_projects/MPI_Project2/residences.dat");

// populate datavector
std::vector<Foodbank> foodbankData((std::istream_iterator<Foodbank>(foodbankFile)),
std::istream_iterator<Foodbank>());
//std::vector<Residence> residenceData((std::istream_iterator<Residence>(residenceFile)),
//std::istream_iterator<Residence>());


std::vector<double> distancess;


string file_contents;
Residence res;
int numProcs = 1;
int recCount = 0;



//pseudo code that I trying to implement
// While(there are more records){
// If record count % numProcs == myID
// ProcessRecord
// else
// Increment file stream pointer forward one record without processing
// Increment Record Count
//}

int numLines = 1;
while(!residenceFile.eof())
{


residenceFile >> res.x >>res.y;

//distancess.push_back(populate_distancesVector(res,foodbankData));
if ( recCount % numProcs == numLines)
{
//call the process
distancess.push_back(populate_distancesVector(res,foodbankData));
}
else
++numLines;
++recCount;
}

最佳答案

您很可能得不到任何结果,因为您的文件没有被打开。很可能,您在初始化 fstream 时没有正确指向它.这就是您看不到任何输出的原因。

在对它们进行任何其他操作之前,您应该始终检查您的是否打开(可访问)。要检查它是否已正确打开:

if (foodBankData.is_open()) {
Foodbank f;

while(foodbankData >> f.x >> f.y )
{
cout<< "X"<< f.x << "Y" <<f.y << endl; //don't forget the newline here
}
}
else {
cerr << "Error opening input file!" << endl;
exit(1);//call the error function or something else here.
}

关于c++ - 如何使用 strip 化算法读取 residence.dat 文件 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19871706/

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