######1. c-型array赋值给c++string类型:

    #include <stdio.h>
    #include <string>
    using namespace std;
     int main(int argc, char** argv)
     {
        char szStr[6] = "hello";
        string str = szStr;//传地址方式
        printf("%s\n", szStr);
        return 0;
    } 

另一种方式

char *s = "123";
string str(s);//也可以利用拷贝构造函数初始化`

另外string型中查找字符或短字符串用find()

2.随机文件名生成

tmpna函数可以创建一个临时文件名,放在pszName指向的c-的字符串中,其中包含两个常量 L_tmpnam限制生成字符数,TMP_MAX限制重复生成次数

    #include <cstdio>
    ......
    char* tmpnam_s(char* pszName)</pre>
3.c++文件打开方式
    ios_base::in//打开读取
    ios_base::out//等价于下一个
    ios_base::out|ios_base::trunc//打开以写入,若存在,则截短
    ios_base::out|ios_base::app//打开以写入,只追加
    ios_base::out|ios_base::out//打开读写,在允许的位置写入
    ios_base::out|ios_base::out|ios_base::trunc//打开读写,存在则截短
    c++mode|ios_base::binary//二进制形式打开
    c++mode|ios_base::ate//指定模式打开,并移到文件尾</pre>

二进制下读取写入,write(),read()

4.删除函数remove()

头文件:cstdio 声明:int remove (char * filename);//filename 为const char *

5.seekg()和tell()函数

以下说明内容转自CSDN

对输入流操作:seekg()tellg() 对输出流操作:seekp()tellp() 下面以输入流函数为例介绍用法: seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。 对于第一个参数,可以是正负数值,正的表示向后偏移,负的表示向前偏移。而第二个参数可以是:

ios::beg//表示输入流的开始位置
ios::cur//表示输入流的当前位置
ios::end//表示输入流的结束位置 ` tellg()`函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。

假设文件test.txt为以下内容:

hello,my world name:hehonghua date:20090902

程序为:

    #include <iostream>
    #include <fstream>
    #include <assert.h>
    using namespace std;
    int main()
    {
    ifstream in("test.txt");
    assert(in);
    in.seekg(0,ios::end);       //基地址为文件结束处,偏移地址为0,于是指针定位在文件结束处
    streampos sp=in.tellg(); //sp为定位指针,因为它在文件结束处,所以也就是文件的大小
    cout<<"file size:"<<endl<<sp<<endl;
    in.seekg(-sp/3,ios::end); //基地址为文件末,偏移地址为负,于是向前移动sp/3个字节
    streampos sp2=in.tellg();
    cout<<"from file to point:"<<endl<<sp2<<endl;
    in.seekg(0,ios::beg);        //基地址为文件头,偏移量为0,于是定位在文件头
    cout<<in.rdbuf();             //从头读出文件内容
    in.seekg(sp2);
    cout<<in.rdbuf()<<endl; //从sp2开始读出文件内容
    return 0;
    }

则结果输出:

file size: 45 from file to point: 30 hello,my world name:hehonghua date:20090902</span>

    int main()
    {
    //得到文件大小:C++方式
        ifstream ifs;
        ifs.open("log.txt");
        assert(ifs.is_open());
        ifs.seekg( 0 , std::ios::end );
        cout<<ifs.tellg()<<endl;
        ifs.close();</span>

        // 得到文件大小:C方式
        FILE* fp = fopen("log.txt", "rb");
        assert ( 0 == fseek(fp, 0, SEEK_END));
        unsigned int usize = ftell(fp);
        cout<<usize<<endl;
        fclose(fp);
        return 0;
    }