从文件加载变量
Loading Variable from a file
at
4
5
6
7
#include<iostream>
#include<stdio.h>
#include <fstream>
using namespace std;
int main()
{
int data[4], a, b, c, d, e, f;
ifstream myfile;
myfile.open("tera.txt");
for (int i = 0; i < 4; i++)
{
myfile >> data[i];
}
myfile.close();
a = data[0];
b = data[1];
c = data[2];
d = data[3];
cout << a << "t" << b << "t" << c << "t" << d << "n";
return 0;
}
它也需要并给出垃圾价值。我应该如何使用忽略函数来忽略值。如果有另一个数组给出了BT,则还有一件事,其中包含这样的值:在Bt如何将BT的所有值存储在数组中?
您只需要跳过第一行即可。您还可以添加可选错误处理,否则阅读可能会失败。
if (!myfile)
{
cout << "can't openn";
return 0;
}
string temp;
myfile >> temp;
cout << "first line: " << temp << endl;
for (int i = 0; i < 4; i++)
{
myfile >> data[i];
if (myfile.fail())
{
cout << "errorn";
myfile.clear();
myfile.ignore(1000000, 'n');
}
}
相关文章:
- 视觉C++:在 DLL 加载期间,全局变量初始化顺序是否具有确定性?
- 使用动态加载库时静态变量为空
- C++:类如何将自身加载到内存中以递增全局变量?
- 使用共享指针加载到变量 C++ 中
- 我如何加载多个脚本而没有变量问题
- 托管语言是否锁定本机库的刷新和重新加载变量?
- 为什么主可执行文件和 dlopen 加载的共享库共享命名空间静态变量的一个副本?
- LLVM - 从具有偏移量的指针加载变量
- 是否可以在跨内核线程迁移后强制重新加载 thread_local 变量?
- 等待条件变量是否会加载 CPU 内核
- 加载 Lua 文件并使用变量而不执行函数
- 从字符串加载变量
- 从文件加载变量
- 在C++中从文本文件加载变量
- 将图像从URL加载到变量/ opencv Mat中
- 将游戏数据加载到没有全局变量的池中
- 将系统(char* 命令)的输出加载到变量 c++
- 如何防止编译器优化对从未使用的变量的加载
- 将内存中的8个字符作为打包的单精度浮点数加载到__m256变量中
- 共享库加载时静态变量损坏