The program output Should be:
程序输出为:
The numbers are: 101 102 103 104 105 106 107 108 108 110
号码是:101 102 103 104 105 106 108 108 108 108 110
But my output is:
但是我的输出是:
The numbers are: 0 0 0 0 0 0 0 0 1606416272 32767
这些数字是:0 0 0 0 0 0 0 0 0 1606416272 32767
This is my code:
这是我的代码:
// 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;
}
This is the contents of the TenNumbers.rtf file I'm reading the data from:
这是十位数的内容。我正在读取来自:
101
102
103
104
105
106
107
108
109
110
UPDATE 1: I tried using txt file but the results are similar.
更新1:我尝试过使用txt文件,但是结果是相似的。
The numbers are: 0 0 0 0 0 0 0 0 1573448712 32767
这些数字是:0 0 0 0 0 0 0 0 0 0 0 15734712 32767
UPDATE 2: I found where the issue was. After running if (inputFile.good())
I found out the file was not getting opened.
更新2:我找到了问题所在。运行if (inputFile.good())之后,我发现文件没有被打开。
4 个解决方案
#1
2
Hi I have compiled your code, with the .txt it runs well, without gives the strage numbers that you see. So probably you are opening a file that does not exists, or can not be red.
你好,我已经编译了你的代码,它的。txt运行良好,没有你看到的strage数字。因此,您可能正在打开一个不存在或不能为红色的文件。
// 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;
}
This snippet checks if the file exists, raises an error if not, and uses a vector(more suitable in c++)
这个代码片段检查文件是否存在,如果不存在,将引发错误,并使用一个向量(更适合c++)
#2
1
Your file name has rtf
as suffix. Does it contain any RTF info in it?
你的文件名以rtf为后缀。里面有RTF信息吗?
The error that I see in your code is that you are assuming ARRAY_SIZE
number of int
s were successfully read when you are printing the numbers.
我在您的代码中看到的错误是,您假设在打印数字时,ARRAY_SIZE的int数被成功读取。
Use:
使用:
// 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] << " ";
}
This will, most likely, reveal any problems in reading the data.
这很可能揭示了阅读数据时的任何问题。
#3
1
ARRAY_SIZE
is the number of int
s you allocated in the array; that is, it is the max number of int
s.
ARRAY_SIZE是在数组中分配的int数;也就是说,它是ints的最大值。
count
is the actual number of ints read from the file. So your final loop should go up to count
since that is the number of actual data. So the loop that prints your data should be:
count是从文件中读取的int数。所以最后的循环应该是计数因为这是实际数据的数量。因此,打印数据的循环应该是:
int i;
for (i = 0; i < count; ++i)
cout << numbers[count] << " ";
Or you can walk a pointer:
或者你可以走一个指针:
int *start;
for (start = numbers; (numbers - start) < count; ++numbers)
cout << *numbers << " ";
Also, I think the file extension should be "txt" rather than "rtf", but that doesn't make a difference.
另外,我认为文件扩展名应该是“txt”而不是“rtf”,但这没有什么区别。
#4
0
An RTF file is not just plain text (it's surrounded by markup) and the character encoding may differ, thus resulting in wrong interpretation of the numbers.
RTF文件不仅仅是纯文本(它被标记包围),字符编码也可能不同,从而导致对数字的错误解释。
So, in your reading loop:
所以,在你的阅读循环中
// Read the numbers from the file into the array.
while (count < ARRAY_SIZE && inputFile >> numbers[count]){
count++;
}
the input stream inputFile
by default is skipping white spaces which in your case could be encoded differently, thereby skipped or messed up in some way.
默认情况下输入流inputFile是跳过空白,在您的情况下可以以不同的方式进行编码,从而在某种程度上跳过或混乱。
Note: Try and add a test line that prints the read number before you store it in the array.
注意:尝试添加一个测试行,在将读号存储到数组中之前打印它。
#1
2
Hi I have compiled your code, with the .txt it runs well, without gives the strage numbers that you see. So probably you are opening a file that does not exists, or can not be red.
你好,我已经编译了你的代码,它的。txt运行良好,没有你看到的strage数字。因此,您可能正在打开一个不存在或不能为红色的文件。
// 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;
}
This snippet checks if the file exists, raises an error if not, and uses a vector(more suitable in c++)
这个代码片段检查文件是否存在,如果不存在,将引发错误,并使用一个向量(更适合c++)
#2
1
Your file name has rtf
as suffix. Does it contain any RTF info in it?
你的文件名以rtf为后缀。里面有RTF信息吗?
The error that I see in your code is that you are assuming ARRAY_SIZE
number of int
s were successfully read when you are printing the numbers.
我在您的代码中看到的错误是,您假设在打印数字时,ARRAY_SIZE的int数被成功读取。
Use:
使用:
// 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] << " ";
}
This will, most likely, reveal any problems in reading the data.
这很可能揭示了阅读数据时的任何问题。
#3
1
ARRAY_SIZE
is the number of int
s you allocated in the array; that is, it is the max number of int
s.
ARRAY_SIZE是在数组中分配的int数;也就是说,它是ints的最大值。
count
is the actual number of ints read from the file. So your final loop should go up to count
since that is the number of actual data. So the loop that prints your data should be:
count是从文件中读取的int数。所以最后的循环应该是计数因为这是实际数据的数量。因此,打印数据的循环应该是:
int i;
for (i = 0; i < count; ++i)
cout << numbers[count] << " ";
Or you can walk a pointer:
或者你可以走一个指针:
int *start;
for (start = numbers; (numbers - start) < count; ++numbers)
cout << *numbers << " ";
Also, I think the file extension should be "txt" rather than "rtf", but that doesn't make a difference.
另外,我认为文件扩展名应该是“txt”而不是“rtf”,但这没有什么区别。
#4
0
An RTF file is not just plain text (it's surrounded by markup) and the character encoding may differ, thus resulting in wrong interpretation of the numbers.
RTF文件不仅仅是纯文本(它被标记包围),字符编码也可能不同,从而导致对数字的错误解释。
So, in your reading loop:
所以,在你的阅读循环中
// Read the numbers from the file into the array.
while (count < ARRAY_SIZE && inputFile >> numbers[count]){
count++;
}
the input stream inputFile
by default is skipping white spaces which in your case could be encoded differently, thereby skipped or messed up in some way.
默认情况下输入流inputFile是跳过空白,在您的情况下可以以不同的方式进行编码,从而在某种程度上跳过或混乱。
Note: Try and add a test line that prints the read number before you store it in the array.
注意:尝试添加一个测试行,在将读号存储到数组中之前打印它。