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>
![](/Images/OutliningIndicators/None.gif)
const char *FileName = "hello.txt" ;
![](/Images/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using namespace std ;
char arr[20] ;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//read file
ifstream fin ;
fin.open(FileName, ios_base::in | ios_base::binary) ;
if (fin.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (fin.read((char *)&arr, sizeof(arr)))
cout << arr << endl ;
fin.close() ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//write file
ofstream fout ;
fout.open(FileName, ios_base::out | ios_base::app |ios_base::binary) ;
if (!fout.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cerr << "open file " << FileName << " wrong!" << endl ;
exit(EXIT_FAILURE) ;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << "please input arr:" << endl ;
cin.get(arr, 20) ;
while (arr[0] != '\0')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fout.write((char *)&arr, sizeof(arr)) << flush ;
while (cin.get() != '\n')
continue ;
cout << "please input arr:" << endl ;
cin.get(arr, 20) ;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//read file
fin.clear() ;
fin.open(FileName, ios_base::in | ios_base::binary) ;
if (!fin.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cerr << "open file " << FileName << " wrong!" << endl ;
exit(EXIT_FAILURE) ;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (fin.read((char *)&arr, sizeof(arr)))
cout << arr << endl ;
fin.close() ;
}
![](/Images/OutliningIndicators/InBlock.gif)
return 0 ;
}
char 用cin.get(ch)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
![](/Images/OutliningIndicators/None.gif)
const char *FileName = "hello.txt" ;
![](/Images/OutliningIndicators/None.gif)
int main(int argc, char argv[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using namespace std ;
// char arr[20] ;
string str ;
char ch ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////
//read file
ifstream fin ;
fin.open(FileName, ios_base::in) ;
if (fin.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (fin.get(ch))
cout << ch ;
fin.close() ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////
//write file
ofstream fout ;
fout.open(FileName, ios_base::out | ios_base::app) ;
if (!fout.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cerr << "open file " << FileName << " error" << endl ;
exit(EXIT_FAILURE) ;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << "entet str (blank line to quit):" << endl ;
getline(cin, str) ;
while (str[0] != '\0')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fout << str << endl ;
while (cin.get() != '\n')
continue ;
cout << "entet str (blank line to quit):" << endl ;
getline(cin, str) ;
}
fout.close() ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////////////////////////////////////////////////////////////////////
//read file
fin.clear() ;
fin.open(FileName, ios_base::in) ;
if (!fin.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cerr << "open file " << FileName << " error" << endl ;
exit(EXIT_FAILURE) ;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (fin.get(ch))
cout << ch ;
fin.close() ;
}
return 0 ;
}
![](/Images/OutliningIndicators/None.gif)
fstream finout ;
#include <iostream>
#include <fstream>
#include <cstdlib>
![](/Images/OutliningIndicators/None.gif)
const char *FileName = "hello.txt" ;
![](/Images/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using namespace std ;
char arr[20] ;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//read file
fstream finout ;
finout.open(FileName, ios_base::in | ios_base::binary) ;
if (finout.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
finout.seekg(0) ;
while (finout.read((char *)&arr, sizeof(arr)))
cout << arr << endl ;
finout.close() ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//write file
// ofstream fout ;
finout.clear() ;
finout.open(FileName, ios_base::out | ios_base::app |ios_base::binary) ;
if (!finout.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cerr << "open file " << FileName << " wrong!" << endl ;
exit(EXIT_FAILURE) ;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
finout.seekp(0, ios_base::end) ;
cout << "please input arr:" << endl ;
cin.get(arr, 20) ;
while (arr[0] != '\0')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
finout.write((char *)&arr, sizeof(arr)) ;
finout << flush ;
while (cin.get() != '\n')
continue ;
cout << "please input arr:" << endl ;
cin.get(arr, 20) ;
}
finout.close() ;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//read file
finout.clear() ;
finout.open(FileName, ios_base::in | ios_base::binary) ;
if (!finout.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cerr << "open file " << FileName << " wrong!" << endl ;
exit(EXIT_FAILURE) ;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
finout.seekg(0) ;
while (finout.read((char *)&arr, sizeof(arr)))
cout << arr << endl ;
finout.close() ;
}
![](/Images/OutliningIndicators/InBlock.gif)
return 0 ;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
![](/Images/OutliningIndicators/None.gif)
const char *FileName = "hello.txt" ;
![](/Images/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using namespace std ;
char arr[20] ;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//read file
fstream finout ;
finout.open(FileName, ios_base::in | ios_base::out | ios_base::binary) ;
if (finout.is_open())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
finout.seekg(0) ;
while (finout.read((char *)&arr, sizeof(arr)))
cout << arr << endl ;
// finout.close() ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//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')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
finout.write((char *)&arr, sizeof(arr)) ;
finout << flush ;
while (cin.get() != '\n')
continue ;
cout << "please input arr:" << endl ;
cin.get(arr, 20) ;
}
// finout.close() ;
// }
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////////////////////////////////////
//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 ;
![](/Images/OutliningIndicators/InBlock.gif)
while (finout.read((char *)&arr, sizeof(arr)))
cout << arr << endl ;
finout.close() ;
// }
![](/Images/OutliningIndicators/InBlock.gif)
return 0 ;
}