gpt4 book ai didi

c++ - read int per line c++ 错误需要解决方案

转载 作者:行者123 更新时间:2023-11-30 05:19:50 26 4
gpt4 key购买 nike

嘿伙计们,我需要帮助,因为我阅读的代码似乎无法正常工作,这是代码。问题如图所示,编译器应该显示所有 100 万个 int 值,但似乎我的编写或显示代码有误。它没有显示任何东西,就像它甚至没有阅读一样。 enter image description here enter image description here

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <omp.h>
#include <ctime>
#include <cstdlib>

using namespace std;

int* CreateArray( int*);
void shellSortParallel( int*, int);
void shellSortSequential(int*, int);
void InsertSort( int*, int, int, int);

int main()
{
int array_size = 1000000;
int n=0;
int* arr=new int[array_size];
ifstream fin("OUTPUT1.txt");
if(! fin)
{
cout << "File could not be opened." << endl;
}
else
{
cout << "File Opened successfully!!!. Reading data from file into array" << endl;
int data;
while(fin>>data)
{
arr[n] = data;
n++;
}
cout << "Displaying Array..." << endl << endl;
for(int i = 0; i < array_size; i++)
{
cout << arr[i];
}
}
fin.close();
int length = 1000000;
double endTime = 0, startTime = 0, totalTime = 0;
double start, end;
cout << "Program is now sorting using shell sort" <<endl;
startTime = time(NULL);
start = omp_get_wtime();// Start performance timer 1 run
shellSortParallel(arr, length);//Run the algorithm
end = omp_get_wtime();// End performance timer 1 run
endTime = time(NULL);
totalTime = endTime - startTime;
cout << "This is the time it took to run. " << totalTime << endl;// time in seconds
int stupid = 0;
cin >> stupid;
cout << "Program has completed all tasks!!" << endl;
return 0;
}

void shellSortParallel(int array[], int length)
{
int h;
int j = 0;
int temp = 0;
int i = 0;
for(h =length/2; h > 0; h = h/2)
{
#pragma omp parallel for shared( array, length, h, i) default(none)
for( i = 0; i < h; i++)
{
//int ID = omp_get_thread_num();
InsertSort(array, i, length, h);
}
}
}

void InsertSort(int arr[], int i, int length, int half)
{
//cout << ID << " ";
int temp = 0;
int j = 0;
for (int f = half + i; f < length; f = f + half)
{
j = f;
while(j > i && arr[j-half] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-half];
arr[j-half] = temp;
j = j -half;
}
}
}

这是我要阅读的文件的简短版本。它是每行 1 到 100 万之间的随机数

2377763
88764877846
281327
60
625
86
646127818
14551
2177645
32033
1826761
555173
3415445377
32430
1101

任何帮助将不胜感激,谢谢

最佳答案

通过 if(fin>>data),您不仅在测试,而且从流中检索数据。我建议使用 ifs.good() 进行测试。总的来说,你可以写这样的代码来代替

std::ifstream fin ("OUTPUT1.txt", std::ifstream::in);
char c = fin.get();

while (fin.good())
{
std::cout << c;
c = fin.get();
}

关于c++ - read int per line c++ 错误需要解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961340/

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