2016-12-04 82 views
1

嘿家伙我需要帮助,我的阅读代码似乎不能正常工作这里的代码。问题如图所示,编译器应该显示所有100万的int值,但似乎我的写或显示代码是错误的。它甚至没有显示出它甚至没有阅读。 enter image description here enter image description here每行读取整数C++错误需要解决方案

#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 

任何帮助将非常感激,

+0

请修正你的代码更大,你必须转换为__int64。所有这些双倍间距,无论从哪里来,都是不可接受的。 –

+0

这是什么'arr [n-1] ='\ 0';'?它不是一个字符数组 – Raindrop7

+0

'32位'int不能保存这个值'88764877846' – Raindrop7

回答

0

arr[n-1] = '\0';是不正确的,因为它不是字符数组,所以不要介意。

加以纠正:

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]; 
} 
  • 分配为什么如此巨大的整数数组?使用矢量是一件好事:

    vector<int> vec; 
    ifstream fin("OUTPUT1.txt"); 
    int data; 
    while(fin >> data) 
    { 
        vec.push_back(data); 
    } 
    
    cout << "Displaying Array..." << endl << endl; 
    for(int i = 0; i < vec.size(); i++) 
        cout << vec[i] << endl; 
    fin.close(); 
    
  • 88764877846出这将导致循环终止阅读,所以你必须要么得到的值作为字符串,然后转换成__int64__int128

整数的带现在

string sLine; 
    int nLines = 0; 
    ifstream fin("OUTPUT1.txt"); 

    // first read to get number of lines 
    while(getline(fin, sLine)) 
     nLines++; 

    //create an array of strings 
    string* pstrValues = new string[nLines]; 

    // set the get pointer to the beginning of file because the previous read moved it 
    fin.clear(); 
    fin.seekg(0, ios::beg); 
    int i = 0; 

    while(getline(fin, sLine)) 
    { 
     pstrValues[i] = sLine; 
     i++; 
    } 

    cout << "Displaying Array..." << endl << endl; 
    for(i = 0; i < nLines; i++) 
     cout << pstrValues[i] << endl; 
    fin.close(); 
  • 你:读值作为字符串有一个字符串数组将其转换为int值,但因为正如我说,有比数值的int(4字节),大小
+0

我试过了你的建议,现在它显示了一堆零。谢谢你的提示虽然:) –

+0

一堆零?? ?? – Raindrop7

+0

我添加第二张照片,所以你可以看到 –

1

通过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(); 
}