c++文件操作(char bin)

istream & seekg(streamoff, ios_base::seekdir);  //streamoff字节为单位
istream & seekg(streampos);                                 //streampos字节为单位

************************************************
ios_base::seekdir取值:

ios_base::beg      文件开始偏移处
ios_base::cur     文件相对当前位子偏移
ios_base::end     文件尾偏移
************************************************

streampos表示的是文件偏移量
如 fin.seekg(1) ;  //指示的是下标为1的字节  这是文件中的第2个字节




bin     用  write() read()

#include <iostream>
#include 
<fstream>
#include 
<cstdlib>

const char *FileName = "hello.txt" ;

int main(int argc, char *argv[])
{
    
using namespace std ;
    
char arr[20] ;

    
//////////////////////////////////////////////////////////////////////////
    //read file
    ifstream fin ;
    fin.open(FileName, ios_base::
in | ios_base::binary) ;
    
if (fin.is_open())
    
{
        
while (fin.read((char *)&arr, sizeof(arr))) 
            cout 
<< arr << endl ;
        fin.close() ;
    }

    
    
//////////////////////////////////////////////////////////////////////////
    //write file
    ofstream fout ;
    fout.open(FileName, ios_base::
out | ios_base::app |ios_base::binary) ;
    
if (!fout.is_open())
    
{
        cerr 
<< "open file " << FileName << " wrong!" << endl ;
        exit(EXIT_FAILURE) ;
    }

    
else
    
{
        cout 
<< "please input arr:" << endl ;
        cin.
get(arr, 20) ;
        
while (arr[0!= '\0')
        
{
            fout.write((
char *)&arr, sizeof(arr)) << flush ;
            
while (cin.get() != '\n')
                
continue ;
            cout 
<< "please input arr:" << endl ;
            cin.
get(arr, 20) ;
        }

    }


    
//////////////////////////////////////////////////////////////////////////
    //read file
    fin.clear() ;
    fin.open(FileName, ios_base::
in | ios_base::binary) ;
    
if (!fin.is_open())
    
{
        cerr 
<< "open file " << FileName << " wrong!" << endl ;
        exit(EXIT_FAILURE) ;
    }

    
else
    
{
        
while (fin.read((char *)&arr, sizeof(arr))) 
            cout 
<< arr << endl ;
        fin.close() ;
    }


    
return 0 ;
}



 

char  用cin.get(ch)

#include <iostream>
#include 
<fstream>
#include 
<string>
#include 
<cstdlib>

const char *FileName = "hello.txt" ;

int main(int argc, char argv[])
{
    
using namespace std ;
    
    
//  char arr[20] ;
    string str ;
    
char ch ;
    
    
//////////////////////////////////////////////////////////////////
    //read file 
    ifstream fin ;
    fin.open(FileName, ios_base::
in) ;
    
if (fin.is_open())
    
{
        
while (fin.get(ch))
            cout 
<< ch ;
        fin.close() ;    
    }

    
    
    
//////////////////////////////////////////////////////////////////
    //write file
    ofstream fout ;
    fout.open(FileName, ios_base::
out | ios_base::app) ;
    
if (!fout.is_open())
    
{
        cerr 
<< "open file " << FileName << " error" << endl ;
        exit(EXIT_FAILURE) ;
    }

    
else 
    
{
        cout 
<< "entet str (blank line to quit):" << endl ;
        getline(cin, str) ;
        
while (str[0!= '\0')
        
{
            fout 
<< str << endl ; 
            
while (cin.get() != '\n')
                
continue ;
            cout 
<< "entet str (blank line to quit):" << endl ;
            getline(cin, str) ;
        }

        fout.close() ;
    }

    
    
///////////////////////////////////////////////////////////////////
    //read file
    fin.clear() ;
    fin.open(FileName, ios_base::
in) ;
    
if (!fin.is_open())
    
{
        cerr 
<< "open file " << FileName << " error" << endl ;
        exit(EXIT_FAILURE) ; 
    }

    
else
    
{
        
while (fin.get(ch))
            cout 
<< ch ;
        fin.close() ;
    }

    
    
return 0 ;
}



fstream finout ;

#include <iostream>
#include 
<fstream>
#include 
<cstdlib>

const char *FileName = "hello.txt" ;

int main(int argc, char *argv[])
{
    
using namespace std ;
    
char arr[20] ;

    
//////////////////////////////////////////////////////////////////////////
    //read file
    fstream finout ;
    finout.open(FileName, ios_base::
in | ios_base::binary) ;
    
if (finout.is_open())
    
{
        finout.seekg(
0) ;
        
while (finout.read((char *)&arr, sizeof(arr))) 
            cout 
<< arr << endl ;
        finout.close() ;
    }

    
    
//////////////////////////////////////////////////////////////////////////
    //write file
//    ofstream fout ;
    finout.clear() ;
    finout.open(FileName, ios_base::
out | ios_base::app |ios_base::binary) ;
    
if (!finout.is_open())
    
{
        cerr 
<< "open file " << FileName << " wrong!" << endl ;
        exit(EXIT_FAILURE) ;
    }

    
else
    
{
        finout.seekp(
0, ios_base::end) ;
        cout 
<< "please input arr:" << endl ;
        cin.
get(arr, 20) ;
        
while (arr[0!= '\0')
        
{
            finout.write((
char *)&arr, sizeof(arr)) ; 
            finout 
<< flush ;
            
while (cin.get() != '\n')
                
continue ;
            cout 
<< "please input arr:" << endl ;
            cin.
get(arr, 20) ;
        }

        finout.close() ;
    }


    
//////////////////////////////////////////////////////////////////////////
    //read file
    finout.clear() ;
    finout.open(FileName, ios_base::
in | ios_base::binary) ;
    
if (!finout.is_open())
    
{
        cerr 
<< "open file " << FileName << " wrong!" << endl ;
        exit(EXIT_FAILURE) ;
    }

    
else
    
{
        finout.seekg(
0) ;
        
while (finout.read((char *)&arr, sizeof(arr))) 
            cout 
<< arr << endl ;
        finout.close() ;
    }


    
return 0 ;
}


 

#include <iostream>
#include 
<fstream>
#include 
<cstdlib>

const char *FileName = "hello.txt" ;

int main(int argc, char *argv[])
{
    
using namespace std ;
    
char arr[20] ;

    
//////////////////////////////////////////////////////////////////////////
    //read file
    fstream finout ;
    finout.open(FileName, ios_base::
in | ios_base::out | ios_base::binary) ;
    
if (finout.is_open())
    
{
        finout.seekg(
0) ;
        
while (finout.read((char *)&arr, sizeof(arr))) 
            cout 
<< arr << endl ;
//        finout.close() ;
    }

    
    
//////////////////////////////////////////////////////////////////////////
    //write file
//    ofstream fout ;
    finout.clear() ;
//    finout.open(FileName, ios_base::out | ios_base::app |ios_base::binary) ;
//    if (!finout.is_open())
//    {
//        cerr << "open file " << FileName << " wrong!" << endl ;
//        exit(EXIT_FAILURE) ;
//    }
//    else
//    {
        finout.seekp(0, ios_base::end) ;
        cout 
<< "please input arr:" << endl ;
        cin.
get(arr, 20) ;
        
while (arr[0!= '\0')
        
{
            finout.write((
char *)&arr, sizeof(arr)) ; 
            finout 
<< flush ;
            
while (cin.get() != '\n')
                
continue ;
            cout 
<< "please input arr:" << endl ;
            cin.
get(arr, 20) ;
        }

//        finout.close() ;
//    }

    
//////////////////////////////////////////////////////////////////////////
    //read file
    finout.clear() ;
//    finout.open(FileName, ios_base::in | ios_base::binary) ;
//    if (!finout.is_open())
//    {
//      cerr << "open file " << FileName << " wrong!" << endl ;
//        exit(EXIT_FAILURE) ;
//    }
//    else
//    {
        finout.seekg(0, ios_base::beg) ;
     
        cout 
<< finout.tellg() << endl ;

        
while (finout.read((char *)&arr, sizeof(arr))) 
            cout 
<< arr << endl ;
        finout.close() ;
//    }

    
return 0 ;
}
posted @ 2007-04-23 20:23  Edward Xie  阅读(2257)  评论(0编辑  收藏  举报