2015-09-06 107 views
2

程序輸出應該是:讀取數據到一個數組

的數字是:101 102 103 104 105 106 107 108 108 110

但我的輸出是:

的數字是:0 0 0 0 0 0 0 0 32767 1606416272

這是我的代碼:

// This program reads data from a file into an array. 

#include <iostream> 
#include <fstream> // To use ifstream 
using namespace std; 

int main() 
{ 
    const int ARRAY_SIZE = 10; // Array size 
    int numbers[ARRAY_SIZE]; // Array number with 10 elements 
    int count = 0;    // Loop counter variable 
    ifstream inputFile;  // Input file stream object 

    // Open the file. 
    inputFile.open("TenNumbers.rtf"); 

    // Read the numbers from the file into the array. 
    while (count < ARRAY_SIZE && inputFile >> numbers[count]){ 
     count++; 
    } 

    // Close the file. 
    inputFile.close(); 

    // Display the numbers read: 
    cout << "The numbers are: "; 
    for (count = 0; count < ARRAY_SIZE; count++){ 
     cout << numbers[count] << " "; 
    } 

    cout << endl; 

    return 0; 
} 

這是TenNumbers.rtf文件,我從讀取數據的內容:

101 
102 
103 
104 
105 
106 
107 
108 
109 
110 

更新1: 我用txt文件,但結果嘗試是相似的。

的數字是:0 0 0 0 0 0 0 0 1573448712 32767

更新2: 我發現那裏的問題了。在運行if (inputFile.good())後,我發現文件沒有打開。

+0

檢查inputFile'的'狀態count]'操作,以確保從文件加載時沒有錯誤。還要考慮如果文件包含少於「ARRAY_SIZE」的數字會發生什麼。我還建議使用'std :: array'或'std :: vector',它們在良好的調試器中執行更多的檢查。 –

+0

我真的不知道.rtf格式。你確定它以ASCII兼容格式存儲文本?如果不是,那可能是問題所在。快速瀏覽一下wiki頁面可以發現,它至少可能包含額外的格式化字符。嘗試使用正常的.txt文件。 –

+0

我試過使用txt文件,但結果是相似的。 '數字是:0 0 0 0 0 0 0 0 1573448712 32767' –

回答

2

嗨我已編譯你的代碼,與它運行良好的.txt,沒有給你看到的數字。 所以很可能你打開的文件不存在,或不能是紅色的。

// This program reads data from a file into an array. 

#include <iostream> 
#include <fstream> // To use ifstream 
#include <vector> 
using namespace std; 

int main() 
{ 
    std::vector<int> numbers; 
    ifstream inputFile("c.txt");  // Input file stream object 

    // Check if exists and then open the file. 
    if (inputFile.good()) { 
     // Push items into a vector 
     int current_number = 0; 
     while (inputFile >> current_number){ 
      numbers.push_back(current_number); 
     } 

     // Close the file. 
     inputFile.close(); 

     // Display the numbers read: 
     cout << "The numbers are: "; 
     for (int count = 0; count < numbers.size(); count++){ 
      cout << numbers[count] << " "; 
     } 

     cout << endl; 
    }else { 
     cout << "Error!"; 
     _exit(0); 
    } 

    return 0; 
} 

這個片段檢查文件是否存在,提高如果沒有錯誤,並且打開和每個`INPUTFILE >>號碼[後使用的載體(更適合在C++)

1

您的文件名有rtf作爲後綴。它是否包含任何RTF信息?

我在您的代碼中看到的錯誤是,您假設ARRAY_SIZE當您打印數字時成功讀取了int的數字。

用途:

// Display the numbers read: 
cout << "Number of ints read: " << count << std::endl; 
cout << "The numbers are: "; 
for (int i = 0; i < count; i++){ 
    cout << numbers[i] << " "; 
} 

這意志,最有可能的,揭示了讀取數據的任何問題。

+0

快速注意:除非不使用namespace std,否則不要使用count作爲變量名稱; – anon

1

ARRAY_SIZE是您在數組中分配的數量int;也就是說,它是最大數量int s。

count是從文件中讀取的實際整數值。因此,你的最終循環應該高達count,因爲那是實際數據的數量。因此,打印數據的循環應該是:

int i; 
for (i = 0; i < count; ++i) 
    cout << numbers[count] << " "; 

或者你可以走了指針:

int *start; 

for (start = numbers; (numbers - start) < count; ++numbers) 
    cout << *numbers << " "; 

另外,我覺得文件的擴展名應爲「TXT」,而不是「RTF格式」,但這並沒有什麼區別。

0

An RTF文件不僅僅是純文本(它被標記包圍),並且字符編碼可能不同,從而導致錯誤地解釋數字。

所以,在你閱讀循環:

// Read the numbers from the file into the array. 
while (count < ARRAY_SIZE && inputFile >> numbers[count]){ 
    count++; 
} 

輸入流inputFile默認是跳過而你的情況可以有不同的編碼,從而跳過或以某種方式搞砸空格。

注意:嘗試並添加一個測試線,它將讀取的數字打印出來,然後將其存儲在數組中。