IO fio
1>基本io
////////////////////////////////////////////////
////////////////////////////////////////////////
ofstream fout; //写出流
fout.open("file.txt");
ofstream fout("file.txt");
***********************
ifstream fin; //读入流
fin.open("file.txt");
ifstream fin("file.txt");
***********************
2>状态流检查与is_open()
/////////////////////////////////////////////////
/////////////////////////////////////////////////
if(fin.fail())
if(!fin.good())
if(!fin)
if(!fin.is_open())
3>打开多个文件及重复打开文件
/////////////////////////////////////////////////
/////////////////////////////////////////////////
ifstream fin;
fin.open("txA.txt");
…
fin.clear(); //清除流状态
fin.close();
…
fin.open("txA.txt"); //再次打开
…
fin.clear(); //清除流状态
fin.close();
…
fin.open("txB.txt"); //打开新得文件
…
fin.close();
4>命令行处理
//////////////////////////////////////////////////
//////////////////////////////////////////////////
int main(int argc, char *argv[]);
//ep:
// count.cpp -- counting characters in a list of files
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <process.h>
// #include <console.h> // for Macintosh
int main(int argc, char * argv[])
{
using namespace std;
// argc = ccommand(&argv); // for Macintosh
if (argc == 1) // quit if no arguments
{
cerr << "Usage: " << argv[0] << " filename[s]\n";
exit(EXIT_FAILURE);
}
ifstream fin; // open stream
long count;
long total = 0;
char ch;
for (int file = 1; file < argc; file++)
{
fin.open(argv[file]); // connect stream to argv[file]
if (!fin.is_open())
{
cerr << "Could not open " << argv[file] << endl;
fin.clear();
continue;
}
count = 0;
while (fin.get(ch))
count++;
cout << count << " characters in " << argv[file] << endl;
total += count;
fin.clear(); // needed for some implementations
fin.close(); // disconnect file
}
cout << total << " characters in all files\n";
system("pause");
return 0;
}
5>文件模式
////////////////////////////////////////////////
////////////////////////////////////////////////
常量 含义
ios_base::in 打开文件读取
ios_base::out 打开文件写入
ios_base::ate 打开文件移动到文件尾
ios_base::app 追加到文件尾
ios_base::trunc 文件存在则覆盖截短文件
ios_base::binary 二进制文件
追加文件
fout.open("file.txt", ios_base::out | ios_base::app);
二进制
fout.open("file.dat",ios_base::out | ios_base::app |ios_base::binary);
//用fout.write((char*)&pl, sizeof(pl)) 用.write()写 .read()读
6>随机存取
///////////////////////////////////////////////////
///////////////////////////////////////////////////
fstream 继承来的两个方法 seekg()用于读入 seekp()用于写出
控制指针在缓冲区中的指向位子
basic_istream<charT, traits>& seekg(off_type, ios_base::seekdir);
basic_istream<charT, traits>& seekg(pos_type);
istream & seekg(streamoff, ios_base::seekdir); //streamoff字节为单位
istream & seekg(streampos); //streampos字节为单位
************************************************
ios_base::seekdir取值:
ios_base::beg 文件开始偏移处
ios_base::cur 文件相对当前位子偏移
ios_base::end 文件尾偏移
************************************************
7>内核格式化
////////////////////////////////////////////////////
////////////////////////////////////////////////////
#include <sstream>
istringstream ostringstream
////////////////////////////////////////////////////
// strout.cpp -- incore formatting (output)
#include <iostream>
#include <sstream>
#include <string>
int main()
{
using namespace std;
ostringstream outstr; // manages a string stream
string hdisk;
cout << "What's the name of your hard disk? ";
getline(cin, hdisk);
int cap;
cout << "What's its capacity in GB? ";
cin >> cap;
// write formatted information to string stream
outstr << "The hard disk " << hdisk << " has a capacity of "
<< cap << " gigabytes.\n";
string result = outstr.str(); // 写到string
cout << result;
return 0;
}
////////////////////////////////////////////////////
// strin.cpp -- formatted reading from a char array
#include <iostream>
#include <sstream>
#include <string>
int main()
{
using namespace std;
string lit = "It was a dark and stormy day, and "
" the full moon glowed brilliantly. ";
istringstream instr(lit); // 从string读入到流
string word;
while (instr >> word)
cout << word << endl;
return 0;
}
8>随机文件名函数
////////////////////////////////////////////////////
////////////////////////////////////////////////////
#include <cstdlib>
char *tmpnam(char *pszName);
//L_tmpnam每个文件字符数 TMP_MAX最多升成的文件名数
/////////////////////////////////////////
//tmpnam.cpp
#include <cstdlib>
#include <iostream>
int main()
{
using namespace std;
cout << TMP_MAX << L_tmpnam <<endl;
char pszName[L_tmpnam] = {'\0'};
for (int i = 0; 10 > i; i++)
{
tmpnam(pszName);
cout << pszName << endl;
}
return 0;
}