从文件读取数据到数组 [英] Reading data from file into an array

查看:68
本文介绍了从文件读取数据到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序输出应为:

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

The numbers are: 101 102 103 104 105 106 107 108 108 110

但是我的输出是:

数字为:0 0 0 0 0 0 0 0 1606416272 32767

The numbers are: 0 0 0 0 0 0 0 0 1606416272 32767

这是我的代码:

// 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文件的内容:

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

更新1:我尝试使用txt文件,但结果相似.

UPDATE 1: I tried using txt file but the results are similar.

数字为:0 0 0 0 0 0 0 0 1573448712 32767

The numbers are: 0 0 0 0 0 0 0 0 1573448712 32767

更新2:我找到了问题所在.运行 if(inputFile.good())后,我发现文件没有打开.

UPDATE 2: I found where the issue was. After running if (inputFile.good()) I found out the file was not getting opened.

推荐答案

我编译了您的代码,.txt运行良好,没有给出您看到的阶段编号.因此,可能是您打开的文件不存在或不能为红色.

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.

// 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;
}

此代码段检查文件是否存在,如果不存在,则引发错误,并使用向量(更适合c ++)

This snippet checks if the file exists, raises an error if not, and uses a vector(more suitable in c++)

这篇关于从文件读取数据到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆