C 编程从外部输入文件定义数组大小
c++ programming define array size from the external input file?
我想从外部输入定义数组大小。
我创建像这样的代码
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void loadIt(int[],int);//loadIT prototype
int main()
{
ofstream outfile("C:\sir.txt");
if (!outfile){
cerr << "Output file could not be opened" << endl;
exit(1);}
outfile << setiosflags(ios::fixed|ios::showpoint);
outfile << setprecision(3);
outfile << setiosflags(ios::fixed|ios::showpoint);
outfile << setprecision(3);
const int arraySize = 20; //here i want the input from the external file
int a[arraySize];
loadIt(a, arraySize);//call to loadIT
}
我想从外部文件定义数组大小..plz帮助我
不确定您想要什么,但是以下可能会有所帮助:
std::ifstream in("C:\input.txt");
int size;
in >> size;
std::vector<int> a(size);
假设input.txt类似于
42
// Possible other values.
内置数组类型的大小固定,因此您只能使用编译时常数来指定大小。您需要一个动态数组,该数组由std::vector
提供:
#include <vector>
const int arraySize = read_array_size_from_wherever();
std::vector<int> a(arraySize);
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
{
const char * fn = "stat.c";
struct stat buf;
stat ( fn, &buf );
std::cout << "File size: " << buf.st_size << std::endl;
int * a = new int[buf.st_size];
// Do your processing
delete[] a;
return ( 0 );
}
相关文章:
- C++编程从外部文本文件定义数组大小
- 如何使用数组 (C++) 读取具有未定义数量的值的 .txt 文件?
- 数组大小由输入文件中的行数定义
- 在另一个文件中初始化的 extern 数组中未定义的引用
- 为什么我的 2D 数组未定义?
- 通过自定义分隔符从文件中读取对象数组
- shared_ptr<>到数组自定义删除器(带make_shared)
- const 数组,用于在数组长度定义中使用其元素或为模板参数提供值
- 在不同的文件(C++)中定义指针数组长度
- 如何使用类 .h 文件中的静态常量来定义数组的长度
- 如何将自定义数据类型(用std::对填充的std::数组)写入文件流
- C++数组类定义和实现,未在作用域中声明
- 在头文件的数组中定义资源 ID
- 如何处理C++中在数组上定义的函数
- c++和Java数组声明/定义:差异
- 读取二维数组文件
- C++ 我知道数组长度,但想知道是否是一种更简洁的数组元素定义方法
- 使用C++数组输入定义重载函数是安全的
- 堆积或堆叠数组.用户定义的类型难题
- 通过无符号整数读取无符号字符数组未定义,因此不安全