2015-09-06 96 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默认是跳过而你的情况可以有不同的编码,从而跳过或以某种方式搞砸空格。

注意:尝试并添加一个测试线,它将读取的数字打印出来,然后将其存储在数组中。