阅读32 返回首页    go 阿里云 go 技术社区[云栖]


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

  上一篇:go 中国黑客谱系
  下一篇:go 上天入地影无踪:十大超级老牌黑客