32
技術社區[雲棲]
C++文件操作
//--------------------第七章 C++的I/O流類庫-------------------------------
/*C++中預定義的流對象:
* cin --與標準輸入設備相關聯,通常是鍵盤
* cout --與標準輸出設備相關聯,通常是屏幕
* cerr --與標準錯誤輸出設備相關聯,屬於非緩衝方式
* clog --與標準錯誤輸出設備相關聯,屬於緩衝方式
*/
/*輸出錯誤時,建議使用cerr,信息送到就馬上輸出,這樣才能及時
* 看到錯誤信息。
* cerr、clog的使用方式與cout的使用方式一樣。
* cerr << "這裏出錯了!" << endl;
*/
//------------------------------------------------------------------
/*輸入輸出的格式控製
一、使用ios類的成員函數進行格式控製
狀態標誌字:
enum {
skipws = 0x0001, //跳過輸入中的空白,可用於輸入
left = 0x0002, //左對齊輸出,可用於輸出
right = 0x0004, //右對齊輸出,可用於輸出
internal = 0x0008, //在符號位和基指示符後填入字符,可用於輸出
dec = 0x0010, //轉換基數為十進製,可用於輸入或輸出
oct = 0x0020, //轉換基數為八進製,可用於輸入或輸出
hex = 0x0040, //轉換基數為十六進製,可用於輸入或輸出
showbase = 0x0080, //輸出時顯示基數指示符,可用於輸入或輸出
showpoint = 0x0100, //輸出時顯示小數點,用於輸出
uppercase = 0x0200, //輸出時把表示進製的字母全都用大寫表示
showpos = 0x0400, //輸出時顯示正數前麵的“+”號
scientific= 0x0800, //用科學記數法表示浮點數
fixed = 0x1000, //用定點形式顯示浮點數
unitbuf = 0x2000, //在輸出操作後立即刷新所有流,可用於輸出
stdio = 0x4000, //在輸出操作後刷新stdout和stderr,可用於輸出
};
*/
//----------------------------------------------------------------------
//-----------------ios類的格式控製成員函數-----------------------------
/*
* long ios::setf(long flags); //設置狀態標誌flags
* long ios::unsetf(long flags); //取消該狀態的設置
* long ios::flags(); //測試狀態標誌
* long ios::flags(long flags); //設置標誌flags,並返回設置前標誌
* int ios::width(); //返回當前的寬度設置值
* int ios::width(int w); //設置域寬w,返回當前的設置
* int ios::precision(int p); //設置小數位數p,返回以前的小數位數
* char ios::fill(); //返回當前填充的字符
* char ios::fill(char ch); //設置填充字符ch,返回當前的填充字符
*/
//----------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpos | ios::scientific); //如果要指定多個格式,用 "|"隔開
cout << 567 << " " << 567.89 << endl;
return 0;
}
//運行結果為:+567 +5.678900e+002
//-------------------iomanip.h--------------------------------
/*
* dec、hex、oct、分別是以十、十六、八進製輸入或輸出
* ws --用於在輸入時路過空白符
* endl --插入一個換行符並刷新輸出流,僅用於輸出
* ends --插入一個空字符,通常用來結束一個字符串,僅用於輸出
* flush --刷新一個輸出流,僅用於輸出
* setbase(int n) --設置進製類型
* resetiosflags(long f) --取消狀態f的設置
* setiosflags(long f) --設置狀態
* setfill(int c) --設置c為填充字符,默認為空格
* setprecision(int b) --設置數據小數部分的位數
* setw(int n) --設置域寬為n
/
//--------------可以選擇的標誌為以下這些------------------------
/*
* ios::left、ios::right、ios::scientific、ios::fixed、ios::dex
* ios::hex、 ios::oct 、ios::uppercase 、ios::showbase
* ios::showpos、ios::showpoint
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << 123 //setw()設置的域寬僅對緊接著的數據有效
<< 567 << endl;
cout << 123 << setiosflags(ios::scientific) << setw(20)
<< 123.456789 << endl;
cout << 123 << setw(10) << hex << 123 << endl;
cout << 123 << setw(10) << oct << 123 << endl;
cout << 123 << setw(10) << dec << 123 << endl;
cout << resetiosflags(ios::scientific) << setprecision(4)
<< 123.456789 << endl;
cout << setiosflags(ios::left) << setfill('#') << setw(8)
<< 123 << endl;
cout << resetiosflags(ios::left) << setfill('$') << setw(8)
<< 456 << endl;
return 0;
}
/*運行結果:
123567
123 1.234568e+002
123 7b
7b 173
173 123
123.5
123#####
$$$$$456
*/
#include <iostream>
using namespace std;
class coord
{
private:
int x, y;
public:
coord(int i = 0, int j = 0)
{
x = i;
y = j;
}
//重載<<運算符
friend ostream & operator<< (ostream & out, coord &c);
};
ostream & operator<< (ostream & out, coord & c)
{
out << c.x << "," << c.y << endl;
return out;
}
int main()
{
coord a(55, 66), b(100, 200);
cout << a << b;
return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
//參數1:指定文本文件,可以是絕對路徑
//參數2:指定打開方式,默認為ios::in
//參數3:指定打開的文件是什麼類型,默認為普通文件,即0
//這裏使用構造函數來打開文件,還可以使用其他方式
//如:open()成員函數
//打開方式:ios::app ios::ate ios::in ios::out
//ios::nocreate ios::noreplace ios::out ios::trunc
//ios::binary
ofstream out("a.txt", ios::out, 0);
if (!out) //if (in.fail())等價
{
cerr << "Open file error!\n";
exit(0); //正常退出
}
out << 10 << " " << 123.456 << " This is a text file\n";
out.close(); //關閉文件
ifstream in("a.txt");
if (in.fail())
{
cerr << "Open file error\n";
exit(0);
}
int nNum;
double dNum;
char str[20];
in >> nNum >> dNum >> str;
cout << nNum << " " << dNum << " "
<< str << endl;
in.close();
return 0;
}
//用get()和put()讀寫二進製文件
//istream &get(unsigned char &ch);
//ostream &put(char ch);
#include <iostream>
#include <fstream>
using namespace std;
void testWrite()
{
ofstream out("D:\\test.dat");
char c = 'a';
for (int i = 0; i < 26; i++)
{
out.put(c);
c++;
}
out.close();
}
void testRead()
{
ifstream in("D:\\test.dat");
char c;
while (in.get(c)) //如果遇到文件尾,返回流對象的值為零
cout << c;
in.close();
}
int main()
{
testWrite();
testRead();
return 0;
}
-------------------------------------------------------------
//用read()和write()讀寫二進製文件
//istream &read(unsigned char *buf, int num);
//ostream &write(const unsigned char *buf, int num);
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
ofstream out("test");
if (!out)
{
cerr << "Cannot open output file!\n";
exit(0);
}
double num = 100.45;
char str[] = "This is a test.";
out.write((char *)&num, sizeof(double));
out.write(str, sizeof(str));
out.close();
return 0;
}
-----------------------------------------------------------
//文件的隨機訪問
//ostream & seekp(streamoff offset, ios::seek_dir dir);
//istream & seekg(streamoff offset, ios::seek_dir dir);
//dir的取值為:
//ios::beg 從文件開始
//ios::cur 從當前文件位置開始
//ios::end 從文件尾開始
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fs("D:\\test", ios::in | ios::out);
if (fs.fail())
{
cerr << "Cannot open file!" << endl;
return 0;
}
else
{
fs.seekp(4, ios::beg);
fs.put('X');
char contents[10];
fs.seekg(0, ios::beg);
fs.get(contents, 10);
cout << contents << endl;
}
return 0;
}
最後更新:2017-04-02 15:15:11