2016-12-04 73 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(); 
}