zoukankan      html  css  js  c++  java
  • C&C++&opencv文件操作

    1. C

    头文件:stdio.h
    相关函数:

    
     1. fopen()
     2. fclose()
     3. fgetc(FILE *fp)
     4. fputc(int ch,FILE *fp)
     5. fgets(char *str, int n, FILE *fp)
     6. fputs()
     7. fprintf(FILE *fp,char *format,arg_list)
     8. fread(void *buffer,unsigned sife,unsigned count,FILE *fp)
     9. fwrite(void *buffer,unsigned sife,unsigned count,FILE *fp)
     10. getw(FILE *fp)
     11. putw(int n,FILE *fp)
     12. feof(FILE *fp)
     13. ferror(FILE *fp)
     14. clearerr(FILE *fp)
     15. fseek(FILE *fp,long offset,int base)
     16. 
    
    
    
    

    文件打开方式:

    例子:将一个文件的内容复制到另一个文件中去。

    #include <stdio.h>  
    void main(int argc,char *argv[]) //命令行参数  
    {  
       int ch;  
        FILE *in,*out; //定义in和out两个文件类型指针  
        if(argc!=3) //判断命令行是否正确  
        {  
    
            printf("Error in format,Usage: copyfile filename1 filename2
    ");  
            return; //命令行错,结束程序的执行  
    
        }  
       //按读方式打开由argv[1]指出的文件  
        if((in=fopen(argv[1],"r"))==NULL)  
    
        {  
            printf("The file <%s> can not be opened.
    ",argv[1]);  
            return; //打开失败,结束程序的执行  
    
        }  
        //成功打开了argv[1]所指文件,再  
        //按写方式打开由argv[2]指出的文件  
        if((out=fopen(argv[2],"w"))==NULL)  
        {  
            printf("The file %s can not be opened.
    ",argv[2]);  
            return; //打开失败,结束程序的执行  
        }  
        //成功打开了argv[2]所指文件  
        ch=fgetc(in); //从in所指文件的当前指针位置读取一个字符  
        while(ch!=EOF) //判断刚读取的字符是否是文件结束符  
        { 
            fputc(ch,out); //若不是结束符,将它写入out所指文件  
            ch=fgetc(in); //继续从in所指文件中读取下一个字符  
        } //完成将in所指文件的内容写入(复制)到out所指文件中  
        fclose(in); //关闭in所指文件  
        fclose(out); //关闭out所指文件  
    }  
    

    将一个文件的内容附加到另一个文件中去。

    #include <stdio.h>  
    #define SIZE 512  
    void main(int argc,char *argv[])  
    {  
        char buffer[SIZE];  
        FILE *fp1,*fp2;  
        if(argc!=3)  
       {  
            printf("Usage: linkfile filename1 filename2
    ");  
            return;  
        }  
        // 按追加方式打开argv[1] 所指文件  
        if((fp1=fopen(argv[1],"a"))==NULL)  
        {  
            printf("The file %s can not be opened.
    ",argv[1]);  
            return;  
        }  
        if((fp2=fopen(argv[2],"r"))==NULL)  
        {  
            printf("The file %s can not be opened.
    ",argv[2]);  
            return;  
        }  
        // 读入一行立即写出,直到文件结束  
        while(fgets(buffer,SIZE,fp1)!=NULL)  
            printf("%s
    ",buffer);  
        while(fgets(buffer,SIZE,fp2)!=NULL)  
            fputs(buffer,fp1);  
        fclose(fp1);  
        fclose(fp2);  
        if((fp1=fopen(argv[1],"r"))==NULL)  
        {  
            printf("The file %s can not be opened.
    ",argv[1]);  
            return;  
        }  
        while(fgets(buffer,SIZE,fp1)!=NULL)  
            printf("%s
    ",buffer);  
        fclose(fp1);  
    }  
    

    博客参考:

    2. C++

    头文件:fstream
    cstdlib
    相关函数:

    
     1. ofstream
     2. ifstream
     3. fstream
     4. open()
     5. istream &seekg(streamoff offset,seek_dir origin);
        ostream &seekp(streamoff offset,seek_dir origin);
     6. close()
     7. ios::app: 以追加的方式打开文件
    ios::ate: 文件打开后定位到文件尾,ios:app就包含有此属性
    ios::binary: 以二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文
    ios::in: 文件以输入方式打开
    ios::out: 文件以输出方式打开
    ios::nocreate: 不建立文件,所以文件不存在时打开失败
    ios::noreplace:不覆盖文件,所以打开文件时如果文件存在失败
    ios::trunc: 如果文件存在,把文件长度设为0
    

    例子:读取文件内容

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    int main ()
    {
        char buffer[256];
        ifstream examplefile("example.txt");
        if (! examplefile.is_open())
        {
           cout << "Error opening file"; exit (1);
        }
        while (!examplefile.eof())
        {
            examplefile.getline(buffer,100);
            cout<<buffer<< endl;
        }
        return 0;
    }
    

    获取二进制文件大小:

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main ()
    {
       const char * filename = "example.txt";
        long l,m;
        ifstream file(filename, ios::in|ios::binary);
        l = file.tellg();
        file.seekg(0, ios::end);
        m = file.tellg();
        file.close();
        cout <<"size of "<< filename;
        cout <<" is "<< (m-l)<<" bytes.
    ";
        return 0;
    }
    

    文件定位:

    file1.seekg(1234,ios::cur);//把文件的读指针从当前位置向后移1234个字节
    file2.seekp(1234,ios::beg);//把文件的写指针从文件开头向后移1234个字节
    

    博客参考:

    3. OPENCV

    文件打开格式要求:.yml/.yaml/.xml
    操作例子

    // file write
    #include "opencv2/opencv.hpp"
    #include <time.h>
    
    using namespace cv;
    using namespace std;
    
    int main(int, char** argv)
    {
        FileStorage fs("test.yml", FileStorage::WRITE);
    
        fs << "frameCount" << 5;
        time_t rawtime; time(&rawtime);
        fs << "calibrationDate" << asctime(localtime(&rawtime));
        Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
        Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
        fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
        fs << "features" << "[";
        for( int i = 0; i < 3; i++ )
        {
            int x = rand() % 640;
            int y = rand() % 480;
            uchar lbp = rand() % 256;
    
            fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
            for( int j = 0; j < 8; j++ )
                fs << ((lbp >> j) & 1);
            fs << "]" << "}";
        }
        fs << "]";
        fs.release();
        return 0;
    }
    

    文件读取:

    // file read
    FileStorage fs2("test.yml", FileStorage::READ);
    
    // first method: use (type) operator on FileNode.
    int frameCount = (int)fs2["frameCount"];
    
    std::string date;
    // second method: use FileNode::operator >>
    fs2["calibrationDate"] >> date;
    
    Mat cameraMatrix2, distCoeffs2;
    fs2["cameraMatrix"] >> cameraMatrix2;
    fs2["distCoeffs"] >> distCoeffs2;
    
    cout << "frameCount: " << frameCount << endl
         << "calibration date: " << date << endl
         << "camera matrix: " << cameraMatrix2 << endl
         << "distortion coeffs: " << distCoeffs2 << endl;
    
    FileNode features = fs2["features"];
    FileNodeIterator it = features.begin(), it_end = features.end();
    int idx = 0;
    std::vector<uchar> lbpval;
    
    // iterate through a sequence using FileNodeIterator
    for( ; it != it_end; ++it, idx++ )
    {
        cout << "feature #" << idx << ": ";
        cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
        // you can also easily read numerical arrays using FileNode >> std::vector operator.
        (*it)["lbp"] >> lbpval;
        for( int i = 0; i < (int)lbpval.size(); i++ )
            cout << " " << (int)lbpval[i];
        cout << ")" << endl;
    }
    fs.release();
    

    博客参考:

    一键三连呀!
  • 相关阅读:
    HDU 3911 线段树区间合并
    Memcache启动&amp;存储原理&amp;集群
    剑指Offer面试题27(Java版):二叉搜索树与双向链表
    ORA-12514: TNS: 监听程序当前无法识别连接描写叙述符中请求的服务
    Java.Lang.NoSuchMethod 错误
    poj2524
    特征价格(Hedonic price)
    特征价格(Hedonic price)
    苏州之行
    苏州之行
  • 原文地址:https://www.cnblogs.com/jee-cai/p/14095318.html
Copyright © 2011-2022 走看看