任何一个文件在使用之前和使用之后, 必须要进行打开和关闭
istream,ostream,cin.get(),cin.getline(),cin.read(),cout.write()
fopen,fgetc(),fputc(),getc(),putc(),fgets(),fputs()
getchar(),putchar(),gets(),puts()
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char infile[255],outputfile[255];
FILE *inf,*outf;
strcpy(filename,m_strYuan);
strcpy(outputfile,m_strYasuo);
inf =fopen(infile,"rb");
outf=fopen(outputfile,"wb");
int ch;
while((ch=fgetc(fp))!=EOF)
{
cal[ch]++;
in_num++;
}
fclose(fp);
fputc(T.leafnum,outf);
fseek(outf,3*T.leafnum,0);
fputc(B.count,outf);
fclose(fp);
=======================================================================================================
ofstream outQuanFile( "权值.txt", ios::out );
for(int j1=0;j1<HN;j1++)
{
outQuanFile<<"U12["<<i1<<"]["<<j1<<"]="<<U12[i1][j1]<<" ";
}
outQuanFile<<"/n";
for(int j3=0;j3<HN;j3++)
{
outQuanFile<<"V["<<i3<<"]["<<j3<<"]="<<V[i3][j3]<<" ";
}
outQuanFile<<"/n";
outQuanFile.close();
========================================================================================================
ifstream GetTrainingData ( "训练样本.txt", ios::in );
for(int m=0;m<N;m++)
{
for(int i=0;i<IN;i++)
{
GetTrainingData>>Study_Data[m].input[i]; //取得输入数据
}
for(int j=0;j<ON;j++)
{
GetTrainingData>>Study_Data[m].teach[j]; //取得输出数据
}
}
GetTrainingData.close();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
在某些场合下,我们需要把输入流当作一个未经解释的字节序来读取,而不是特定的数据类型(如char,int,string)的序列
istream的成员函数get()一次读入一个字节,getline()一次读入一块字节,或者由一个换行符作为结束,或者由某个用户定义的终止字符作为结束.
1. get(char& ch):从输入流中提取一个字符,包括空白字符,并将它存储在ch中.(回车,空格都算)
char ch; while( cin.get(ch) ) { ... }; cout.put(ch);
2. get() :从输入流输入一个字符,它返回字符值而不是并应用的istream对象.返回类型是int而非char.
3. get(char *sink, streamsize size, char delimiter='/n')//(字符数组,最大数目,结束符),在第二个get之前一定要去掉delimiter,cin.ignore();delimiter不会被放在数组中,而是留作istream的下一个字符.
cin.get(line,max_line); get_count=cin.gcount();//实际读入个数 cin.ignore();
*** cin.getline(char *sink, streamsize size, char delimiter='/n'),它自己丢弃delimiter,cin.getline( ... )
cin.read(char *addr, streamsize size);//从输入流提取size个连续的字节,放入addr;
cout.write(const char *sink, streamsize lenth);//(要显示字符个数)
标准库的getline可以输入到一个string对象中
getline(istream &is, string str, char delimiter);
----------------------------------------------------------------------
ofstream outfile("copy.out", ios_base::out);//ios_base::app
试图读写文件之前,先判断它是否已成功打开
if( ! outfile){ cerr<<"cannot open copy.out for output /n"; exit(-1); }
ifstream infile("inputf.txt", ios_base::in);
if(!infile) { cerr<<"cannot open input file /n"};
infile<<name[20]<<score;
outfile<<name[20]<<" "<<score;
----------------------------------------------------------------------
FILE *fopen( const char *fname, const char *mode );//(文件名,打开模式)
"r" 打开一个用于读取的文本文件
"w" 创建一个用于写入的文本文件
"a" 附加到一个文本文件
"rb" 打开一个用于读取的二进制文件
"wb" 创建一个用于写入的二进制文件
1)读写字符
int fgetc(FILE *stream);//返回stream流的下一个字符,整形
int fputc( int ch, FILE *stream );//把字符ch输出到输出流
while( (ch=fgetc(inF)) != EOF) { cout<<ch<<endl; fputc(ch,outF); }
(getc与fgetc相同,put与fputc相同)
2)读写字符串
char *fgets( char *str, int num, FILE *stream );//(字符串,个数,文件)
int fputs( const char *str, FILE *stream ); //(字符串,写入文件)
3)
int getchar( void ); //从STDIN(标准输入)获取并返回下一个字符
int putchar( int ch ); //把ch写到STDOUT(标准输出).
4)
char *gets( char *str );//从STDIN(标准输入)读取字符并把它们加载到str(字符串)里,直到遇到新行(/n)或到达EOF
int puts( char *str ); //把str(字符串)写到STDOUT(标准输出)上