在 c++ 中向/从文件中读取写入变量
Read Write Variable to/from file in c++
我正在尝试让我的c ++程序写入然后从文件中读取变量,但没有成功。代码如下:
#include <iostream>
#include <fstream>
using namespace std;
int f;
int main()
{
ifstream in("variabila.txt");
ofstream fout;
fout.open("variabila.txt");
fout<<f<<endl;
fout.close();
cout << f << endl;
cin >> f;
in >> f;
cout << f << endl;
return 0;
}
你能帮我吗?
关闭文件进行写入后,应打开文件进行读取。
#include <iostream>
#include <fstream>
using namespace std;
const int f = 123;
int main()
{
{
ofstream fout("variabila.txt");
fout<<f<<endl;
}
{
ifstream in("variabila.txt");
int g;
in >> g;
cout << g << endl;
}
return 0;
}
相关文章:
- 从字符串变量中逐字符读取单词
- C++:如何读取分离变量,然后读取向量
- 全局变量 多读取器 一个写入器多线程安全?
- 在指向函数中读取变量时出现分割错误
- 从Objective-C中的C 标头读取变量
- qml从c++读取变量
- 从字符串中读取变量数据
- 如何从另一个类读取变量
- 如何使用 std::getline 来匹配预期的输入并读取变量部分
- 如何从标准向量前端读取并删除读取变量
- 如何从一个对象的传递指针中读取变量
- 正在从C++中的.mat文件中读取变量
- 初始化数组,放置新,读取变量,定义的行为
- 读取变量时发生简单代码错误:无法访问地址处的内存
- GDB:<错误读取变量>,带有 C++ 向量
- 从文件中读取变量时出现问题
- QMutex需要读取变量
- 在多线程环境中读取变量是否总是有效的?
- 正在从配置文件中读取变量
- 与'fgets'同时读取变量