美文网首页我爱编程
文件IO,路径,在Windows和Linux的异同

文件IO,路径,在Windows和Linux的异同

作者: 御史神风 | 来源:发表于2018-08-04 23:31 被阅读0次

    C++ 文件IO 路径 Windows Linux
    测试用系统及编译器:
    windows:windows10 & C-Free
    Linux:kail & g++

    代码

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        ifstream fin;
        ofstream fout;
        string path;
    
        int a, b;
        
        fin.open(filename.in);
        fin >> a >> b;
        cout << "a:" << a << "b:" << b << endl;
        fin.close();
        
        fout.open(filename.out);
        fout << a << " " << b;
        fout.close();
        
        return 0;
    }
    
    

    例子0x0

    path:filename.dat

    系统
    Windows 正常 完全正常
    Linux 正常 完全正常

    例子0x1(唯一出岔子的地方)

    windows式路径
    path:.\filename.dat

    系统
    Windows 正常 完全正常
    Linux 乱码 无输出

    shell

    # ./fio
    a:32722b:1336815288
    

    例子0x2

    linux式路径
    path:./filename.dat

    系统
    Windows 正常! 完全正常
    Linux 正常 完全正常

    总结和感想

    至少在win10中,三种路径都是可用的。而linux中,使用windows式的路径会出错。所以如果数据文件和程序在同一目录,还是用第一种“万金油”路径格式。不然,最好用linux格式(oier的泪),想想在noip省赛因为这个翻车我的心就痛。

    相关文章

      网友评论

        本文标题:文件IO,路径,在Windows和Linux的异同

        本文链接:https://www.haomeiwen.com/subject/vjkvvftx.html