从文本文件中读取整数并将它们存储到数组中

时间:2022-09-06 00:22:04

I am pretty much new to C++ and got this assignment from school to do and now I am stuck as I can't get it to work, some help would be appreciated. Thanks in advance.

我几乎是C ++的新手,从学校开始做这个任务,现在我被困住,因为我无法让它工作,一些帮助将不胜感激。提前致谢。

I have a text file with integers like this: 8 3 7 1 2 0

我有一个像这样的整数的文本文件:8 3 7 1 2 0

I need to create program which reads those integers and stores them in an array. Program checks text file and counts how many integers are there and accordingly creates array of size that is needed then it fills that array with integers from the file. I've came up with something like this:

我需要创建读取这些整数并将它们存储在数组中的程序。程序检查文本文件并计算有多少整数,并相应地创建所需的大小数组,然后用文件中的整数填充该数组。我想出了类似的东西:

#include <iostream>
#include <fstream>
using namespace std;

int main(){

    ifstream File("data.txt");
    int count;
    File >> count;
    int array[count];
    for(int i=0; i<count; i++){
        File >> array[i];
    }

    cout<<"File contains: "<<count<<" integers";
    cout<<"Array of integers taken from the file: ";

    for(int i=0; i<count; i++){
        cout<<array[i];
    }
}

For some reasons my count integer doesn't get any value, I've checked and it's just a zero after program and thus my whole code won't work.

由于某些原因,我的计数整数没有得到任何值,我已经检查过,它只是程序后的零,因此我的整个代码将无法正常工作。

2 个解决方案

#1


0  

If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits<T>::max() or std::numeric_limits<T>::min() is written and failbit flag is set.

如果提取失败,则将零写入值并设置failbit。如果提取结果的值太大或太小而不适合值,则写入std :: numeric_limits :: max()或std :: numeric_limits :: min()并设置failbit标志。

This comes from the documentation of operator>> of std::ifstream. As you can see, if extraction fails, then the variable is set to 0. Why did extraction fail? Well, you never check if the file is open or not, so it is possible that the file doesn't exist and loading it failed.

这来自std :: ifstream的operator >>的文档。如您所见,如果提取失败,则将变量设置为0.为什么提取失败?好吧,你永远不会检查文件是否打开,因此文件可能不存在并且加载失败。

But even if your file is loaded correctly, it doesn't seem to me that the first element in the file is the amount of integers in it. If it is not, you will have to read each integer, and store them in a dynamic array, as you do not know the size (like std::vector.)

但即使你的文件被正确加载,我觉得文件中的第一个元素也不是它中的整数。如果不是,则必须读取每个整数,并将它们存储在动态数组中,因为您不知道大小(如std :: vector。)

#2


0  

Variable lenght array does't exist un c++. Use a vector<int> to store the data.

变量长度数组不存在于c ++中。使用vector 存储数据。

Btw, you can use:

顺便说一下,你可以用:

System("pwd");//linux

To check working path.

检查工作路径。

#1


0  

If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits<T>::max() or std::numeric_limits<T>::min() is written and failbit flag is set.

如果提取失败,则将零写入值并设置failbit。如果提取结果的值太大或太小而不适合值,则写入std :: numeric_limits :: max()或std :: numeric_limits :: min()并设置failbit标志。

This comes from the documentation of operator>> of std::ifstream. As you can see, if extraction fails, then the variable is set to 0. Why did extraction fail? Well, you never check if the file is open or not, so it is possible that the file doesn't exist and loading it failed.

这来自std :: ifstream的operator >>的文档。如您所见,如果提取失败,则将变量设置为0.为什么提取失败?好吧,你永远不会检查文件是否打开,因此文件可能不存在并且加载失败。

But even if your file is loaded correctly, it doesn't seem to me that the first element in the file is the amount of integers in it. If it is not, you will have to read each integer, and store them in a dynamic array, as you do not know the size (like std::vector.)

但即使你的文件被正确加载,我觉得文件中的第一个元素也不是它中的整数。如果不是,则必须读取每个整数,并将它们存储在动态数组中,因为您不知道大小(如std :: vector。)

#2


0  

Variable lenght array does't exist un c++. Use a vector<int> to store the data.

变量长度数组不存在于c ++中。使用vector 存储数据。

Btw, you can use:

顺便说一下,你可以用:

System("pwd");//linux

To check working path.

检查工作路径。