c++ - fstream 对象没有从文件中读取数据?
问题描述
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream student;
student.open("details.txt");
string name,UID,sem;
for(int i=0;i<5;i++)
{
cout<<"Enter the name , UID , semester\n";
cin>>name>>UID>>sem;
student<<name<<" "<<UID<<" "<<sem<<endl;
}
string xyz;
while(getline(student,xyz))
{
cout<<xyz<<"\n";
}
student.close();
}
这显示错误,但如果我让 ifstream 对象写入和 ofstream 对象(与 ifstream 的对象不同)读取,那么只有它被执行。感谢你们对我的帮助
解决方案
<fstream>
包含ofstream
和ifstream
类。
您应该使用ofstream
object 将数据写入文件:
ofstream fileOut("file-name.txt");
fileOut << "data";
fileOut.close();
和ifstream
对象从文件中读取数据:
string data;
ifstream fileIn("file-name.txt");
fileIn >> data;
fileIn.close();
推荐阅读
- python - 通过python发送HTTP请求
- java - 如何在apache中从角度调用spring boot api
- android - android webview中的谷歌登录卡在一个页面中,提示“请稍等”和一个加载进度条
- python-3.x - 从网页上的下拉值中抓取网页
- python - Keras 模型停止训练,但没有说明为什么以及如何启用 GPU 加速
- python - 2019 年 Kinect-Python-OpenCV 的状态
- php - XAMPP localhost - 将操作系统更改为 windows 10 pro 2019 后无法访问 phpmyadmin
- kubernetes - 未能保存输出:等待 Argo Workflow 上的条件超时
- php - 在包含 PHP 中填充下拉列表
- java - 最大化舞台后的场景最大化javafx