基本知识
打开方式
例子
#include <iostream>
#include <fstream>
using namespace std;
void main ( )
{
ofstream outFile ( "outFile.txt" , ios:: out) ;
if ( ! outFile)
cerr<< "Open file or create file error." << endl;
else
outFile << "This is a test file." ;
}
#include <fstream>
void main ( )
{
ofstream outFile;
outFile. open ( "outFile.txt" , ios:: out) ;
if ( ! outFile)
cerr<< "Open file or create file error." << endl;
else
{
outFile << "This is a test file." ;
outFile. close ( ) ;
}
}
#include <fstream.h>
void main ( )
{
ofstream outfile1 ( "myfile1.txt" ) ;
outfile1<< "Hello!...CHINA LIAONING" << endl;
outfile1. close ( ) ;
outfile1. open ( "myfile1.txt" , ios:: app) ;
int x= 1212 , y= 6868 ;
outfile1<< x<< " " << y<< endl;
outfile1. close ( ) ;
char str1[ 80 ] , str2[ 80 ] ;
int x2, y2;
ifstream infile1 ( "myfile1.txt" ) ;
infile1>> str1>> str2;
infile1>> x2>> y2;
infile1. close ( ) ;
cout<< "str1=" << str1<< endl;
cout<< "str2=" << str2<< endl;
cout<< "x2=" << x2<< endl;
cout<< "y2=" << y2<< endl;
}
输入一个字符串写入文件中并统计字符的个数
#include <fstream.h>
void main ( )
{
char str[ 80 ] ;
cout<< "Input string :" << endl;
cin. getline ( str, 80 , '\n' ) ;
ofstream fout ( "ft.txt" ) ;
int i= 0 ;
while ( str[ i] )
fout. put ( str[ i++ ] ) ;
cout<< "len=" << i<< endl;
fout. close ( ) ;
}
从文件中读出所写字符串并统计字符的个数
void main ( )
{
ifstream fin ( "ft.txt" ) ;
char ch;
int i= 0 ;
fin. get ( ch) ;
while ( ! fin. eof ( ) )
{
cout<< ch;
i++ ;
fin. get ( ch) ;
}
cout<< endl<< "len=" << i<< endl;
fin. close ( ) ;
}
#include <fstream.h>
#include <string.h>
struct stu
{
char name[ 20 ] ;
int age;
double score;
} ;
void main ( )
{
char str[ 20 ] = "Hello world!" ;
stu ss= { "zhang san" , 22 , 93.5 } ;
cout<< "WRITE to 'fb.bin'" << endl;
ofstream fout ( "fb.bin" , ios:: binary) ;
int Len= strlen ( str) ;
fout. write ( ( char * ) ( & Len) , sizeof ( int ) ) ;
fout. write ( str, Len) ;
fout. write ( ( char * ) ( & ss) , sizeof ( ss) ) ;
fout. close ( ) ;
cout<< "-------------------------" << endl;
cout<< "READ from 'fb.bin'" << endl;
char str2[ 80 ] ;
ifstream fin ( "fb.bin" , ios:: binary) ;
fin. read ( ( char * ) ( & Len) , sizeof ( int ) ) ;
fin. read ( str2, Len) ;
str2[ Len] = '\0' ;
fin. read ( ( char * ) ( & ss) , sizeof ( ss) ) ;
cout<< "Len=" << Len<< endl;
cout<< "str2=" << str2<< endl;
cout<< "ss=" << ss. name << "," << ss. age << "," << ss. score << endl;
fin. close ( ) ;
}
#include <fstream.h>
#include <string.h>
struct stu
{
char name[ 20 ] ;
int age;
double score;
} ;
void main ( )
{
int n;
stu ss;
cout<< "n=?" ;
cin>> n;
ofstream fout ( "f01.bin" , ios:: binary) ;
for ( int i= 0 ; i< n; i++ )
{
cout<< "name,age,score=?" ;
cin>> ss. name >> ss. age >> ss. score ;
fout. write ( ( char * ) ( & ss) , sizeof ( ss) ) ;
}
fout. close ( ) ;
cout<< "-------------------------" << endl;
ifstream fin ( "f01.bin" , ios:: binary) ;
double ave= 0 ;
int n1= 0 ;
fin. read ( ( char * ) ( & ss) , sizeof ( ss) ) ;
while ( ! fin. eof ( ) )
{
ave+ = ss. score ;
n1++ ;
fin. read ( ( char * ) ( & ss) , sizeof ( ss) ) ;
}
fin. close ( ) ;
cout<< "n1=" << n1<< " ave=" << ave/ n1<< endl;
}