ifstream不支持输出操作,那么打模式中的ifstream::out有什么意义呢?
http://www.cplusplus.com/reference/iostream/ifstream/open/
在cplusplus里看到ifstream的打开模式中有out这个模式。
- C/C++ code
#include <iostream>#include <fstream>using namespace std;int main () { ifstream infile; infile.open ("test.txt", ifstream::in | ifstream::out); int a; infile<<a; //既然不支持 infile>>a; infile.close(); return 0;}[解决办法]
晕 哪有ifstream::out啊
[解决办法]
ofstream::out可以输出,
ifstream::out??不知道,
[解决办法]
ifstream - input file stream哪来的out??
- C/C++ code
#include<iostream>#include<fstream>#include<iomanip>using namespace std;struct student{ char name[10]; int num; double score[3]; double avr;}stud[5];void main(){ double sum = 0.0;//注意这里初始化 int i,j; cout<<"input the information of five student!"<<endl; for(i = 0;i < 5;i++) { cout<<"name:"; cin>>stud[i].name; cout<<"num:"; cin>>stud[i].num; for(j = 0;j < 3;j++) { cout<<"score"<<j+1<<":"; cin>>stud[i].score[j]; sum += stud[i].score[j]; } stud[i].avr = sum /3.0; cout<<endl; } ofstream outfile("stud.dat"); if(!outfile) { cout<<"open error!"<<endl; abort(); } outfile<<"name num socre1 score2 score3 average"<<endl; for(i = 0;i < 5;i++) { outfile<<stud[i].name<<" "<<stud[i].num<<" "; for(j = 0 ;j < 3;j++) outfile<<stud[i].score[j]<<" "; outfile<<stud[i].avr<<endl; } outfile.close();}
[解决办法]
MSDN上的解释
#include <fstream.h>
The ifstream class is an istream derivative specialized for disk file input. Its constructors automatically create and attach a filebuf buffer object.
The filebuf class documentation describes the get and put areas and their associated pointers. Only the get area and the get pointer are active for the ifstream class.
#include <fstream.h>
The ofstream class is an ostream derivative specialized for disk file output. All of its constructors automatically create and associate a filebuf buffer object.
The filebuf class documentation describes the get and put areas and their associated pointers. Only the put area and the put pointer are active for the ofstream class.