Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取文件,然后创建数组函数C++; 我是C++新手,最近我读文件有困难。这个问题现在已经解决了。我现在想做的是使用我读取的文件创建一个数组,但所有这些都必须在函数中完成,这意味着函数必须返回一个数组,以便我可以在主节点上使用它。我遵循的过程是: 读取文件 创建数组 返回数组地址_C++_Arrays_Function - Fatal编程技术网

读取文件,然后创建数组函数C++; 我是C++新手,最近我读文件有困难。这个问题现在已经解决了。我现在想做的是使用我读取的文件创建一个数组,但所有这些都必须在函数中完成,这意味着函数必须返回一个数组,以便我可以在主节点上使用它。我遵循的过程是: 读取文件 创建数组 返回数组地址

读取文件,然后创建数组函数C++; 我是C++新手,最近我读文件有困难。这个问题现在已经解决了。我现在想做的是使用我读取的文件创建一个数组,但所有这些都必须在函数中完成,这意味着函数必须返回一个数组,以便我可以在主节点上使用它。我遵循的过程是: 读取文件 创建数组 返回数组地址,c++,arrays,function,C++,Arrays,Function,在main中:使用函数将数组放入变量中。 到目前为止,我的代码是: #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; void imprimeArreglo (int[],int); int* leeArreglo(string&); int i; void imprimeArreglo(int

在main中:使用函数将数组放入变量中。 到目前为止,我的代码是:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void imprimeArreglo (int[],int);
int* leeArreglo(string&);

int i;
void imprimeArreglo(int a[])
{
    cout << a[i] << endl;
}
int* leeArreglo(string nombre)
{
    ifstream arch(nombre.c_str());
    string tamStr;
    int tam;

    if(arch.is_open())
    {
         getline(arch,tamStr); 
         tam=atoi(tamStr.c_str());
         int arr[tam];
         for(int i=0;i<tam;i++)
         {
             arch >> arr[i];
         }
         int *ret=arr;
         return ret;
     }
     return 0;
}
int main()
{
     string nombre = "arr.txt";  
     int *a= leeArreglo(nombre.c_str());
     imprimeArreglo(a);
     return 0;
}
我认为这应该打印(使用imprimeArreglo(a)):
98223760023100-845
但我在控制台中得到的唯一东西是:
2292592
我认为这是一个内存地址???我做错了什么?有没有更好的方法不使用向量来做这件事
提前谢谢

您正在返回对局部变量的引用。当函数超出范围时,数组arr被释放,因此地址返回只是垃圾。 如果要返回函数调用后仍存在的地址,请执行以下操作:

int *arr = new int[tam];
然后,在调用函数后,地址将是有效的,但就设计而言,这并不重要,因为您将释放调用方上的数组的责任留给了调用方。
我个人会将所有这些都封装在一个类中,并拥有它的数组成员。

您正在返回对局部变量的引用。当函数超出范围时,数组arr被释放,因此地址返回只是垃圾。 如果要返回函数调用后仍存在的地址,请执行以下操作:

int *arr = new int[tam];
然后,在调用函数后,地址将是有效的,但就设计而言,这并不重要,因为您将释放调用方上的数组的责任留给了调用方。
我个人会将所有这些都封装在一个类中,并拥有它的数组成员。

这是一种简单的方法:

#include <iterator>
#include <vector>
#include <fstream>

std::vector<int> fileContents(const std::string& filename)
{
  ifstream inputFile(filename.c_ctr());
  std::istream_iterator<int> end;
  std::istream_iterator<int> it(inputFile);
  return std::vector<int>(it, end);
}
#包括
#包括
#包括
标准::矢量文件内容(常量标准::字符串和文件名)
{
ifstream输入文件(filename.c_ctr());
std::istream_迭代器端;
std::istream\u迭代器it(inputFile);
返回std::vector(it,end);
}

这会将所有数字读入一个向量。您可能需要修改它以忽略第一个
10
,例如通过调用
std::advance(it,1)在初始化
它之后

这是一种简单的方法:

#include <iterator>
#include <vector>
#include <fstream>

std::vector<int> fileContents(const std::string& filename)
{
  ifstream inputFile(filename.c_ctr());
  std::istream_iterator<int> end;
  std::istream_iterator<int> it(inputFile);
  return std::vector<int>(it, end);
}
#包括
#包括
#包括
标准::矢量文件内容(常量标准::字符串和文件名)
{
ifstream输入文件(filename.c_ctr());
std::istream_迭代器端;
std::istream\u迭代器it(inputFile);
返回std::vector(it,end);
}

这会将所有数字读入一个向量。您可能需要修改它以忽略第一个
10
,例如通过调用
std::advance(it,1)
初始化
它之后

好的,经过一些研究和咨询,我得到了答案!谢谢大家帮助我:)。这是使事情发生的代码(使用数组!!)

int*readArray(字符串名称)
{
ifstream fil(name.c_str());
字符串大小;
整数大小;
if(fil.is_open())
{
getline(fil,sizeStr);
大小=原子(sizeStr.c_str());
int*arr=新int[size+1];
对于(int i=1;i>arr[i];
}
//我们将大小存储在第一个位置;)
arr[0]=大小;

//cout好的,经过一些研究和咨询,我得到了答案!谢谢大家帮助我:)。这是使事情发生的代码(使用数组!!)

int*readArray(字符串名称)
{
ifstream fil(name.c_str());
字符串大小;
整数大小;
if(fil.is_open())
{
getline(fil,sizeStr);
大小=原子(sizeStr.c_str());
int*arr=新int[size+1];
对于(int i=1;i>arr[i];
}
//我们将大小存储在第一个位置;)
arr[0]=大小;

//cout a)
intarr[tam]
可变长度数组是一种非标准扩展。b)为什么不使用向量?a)这能解决我的问题吗?b)因为我真的想用数组实现它。你能读一个文件并将其放入向量吗?a)正确缩进代码并获得所有这些该死的指针将是一个好的开始添加一个文件并将其放入一个向量?'是的!使用流中的一个。
typedef std::istream\u迭代器istr\u it;std::copy(istr\u it(arch),is\u it(),std::back\u inserter(arr));
vector与20ish相比只需要两行代码。而且它更快,而且有效。a)
int arr[tam]
可变长度数组是一种非标准扩展。b)为什么不使用向量?a)这能解决我的问题吗?b)因为我真的想用数组实现它。你能读一个文件并将其放入向量吗?a)正确缩进代码并获得所有这些该死的指针将是一个好的开始添加一个文件并将其放入向量?“是的!使用流中的一行。
typedef std::istream\u迭代器istr\u it;std::copy(istr\u it(arch),is\u it(),std::back\u inserter(arr));
vector与您的20ish相比,只需要两行代码。而且它更快,也更有效。我会赞成,但“有没有更好的方法不用向量来完成它吗?”@0x499602D2“来自评论:你能读懂一个文件并将其放入一个向量吗?”–David Merinos“@0x499602D2我也有这样的印象,OP在寻找一个真实世界的验证解决方案,而不是解决一个愚蠢的受限编程学习任务。至少他会有一个可以从向量访问的数组。@MooingDuck我想这是
advance(1),我认为有一个数字<代码> 10 /代码>,这是下一行要阅读的元素的数量。@ 0x499 602D2:即使考虑到这些评论,我仍然认为这是一个很好的答案,也许是最好的答案。这回答了OP甚至不知道他的问题。@0x499602D2“从c