zoukankan      html  css  js  c++  java
  • c++读取 轨迹.txt

    仅做测试用!

    测试1.txt

    按行读入

    1. 只读取一行
    /*************************************************************************
    	> File Name: test.cpp
    	> Author: lihanwen
    	> Mail: 18646139976@163.com
    	> Created Time: 二  4/28 10:32:05 2020
     ************************************************************************/
    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cmath>
    #include <deque>
    #include <fstream>
    
    using namespace std;
    
    string input = "1.txt";
    
    int main() {
        fstream fin(input);
        string a;
        getline(fin, a);
        cout << a << endl;
        return 0;
    }
    

    测试结果:

    测试结果:单独输出一行

    1. 按行读取整个txt
    /*************************************************************************
    	> File Name: test.cpp
    	> Author: lihanwen
    	> Mail: 18646139976@163.com
    	> Created Time: 二  4/28 10:32:05 2020
     ************************************************************************/
    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cmath>
    #include <deque>
    #include <fstream>
    
    using namespace std;
    
    string input = "1.txt";
    
    int main() {
        fstream fin(input);
        string a;
        while (!fin.eof()) {
            getline(fin, a);
            cout << a << endl;
        }
        return 0;
    }
    

    测试结果

    或者是:

    /*************************************************************************
    	> File Name: test.cpp
    	> Author: lihanwen
    	> Mail: 18646139976@163.com
    	> Created Time: 二  4/28 10:32:05 2020
     ************************************************************************/
    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cmath>
    #include <deque>
    #include <fstream>
    
    using namespace std;
    
    string input = "1.txt";
    
    int main() {
        fstream fin(input);
        string a;
        while (getline(fin, a)) {
            cout << a << endl;
        }
        return 0;
    }
    

    测试结果

    按字符串读入

    注意:按字符读入时,如果遇到空格,并且没有循环的情况下,会自动停止。

    /*************************************************************************
    	> File Name: test.cpp
    	> Author: lihanwen
    	> Mail: 18646139976@163.com
    	> Created Time: 二  4/28 10:32:05 2020
     ************************************************************************/
    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cmath>
    #include <deque>
    #include <fstream>
    
    using namespace std;
    
    string input = "1.txt";
    
    int main() {
        fstream fin(input);
        string a;
        fin >> a;
        cout << a << endl;
        return 0;
    }
    

    测试结果

    /*************************************************************************
    	> File Name: test.cpp
    	> Author: lihanwen
    	> Mail: 18646139976@163.com
    	> Created Time: 二  4/28 10:32:05 2020
     ************************************************************************/
    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cmath>
    #include <deque>
    #include <fstream>
    
    using namespace std;
    
    string input = "1.txt";
    
    int main() {
        fstream fin(input);
        string a;
        while (!fin.eof()) {
            fin >> a;
            cout << a << endl;
        }
        return 0;
    }
    

    image-20200428121837488

    如果是按字符读入,需要将输出为char类型即可

    按字符读入,结果需要什么类型,输出类型就是什么

    参考:

    [c++读取txt][https://www.cnblogs.com/VVingerfly/p/4435898.html]

  • 相关阅读:
    apache2三种模式及切换到event模式
    MySQL添加用户、创建数据库、分配权限
    ExcelHelper导出
    C#中将错误写进日志文件
    k3 cloud金蝶云参数设置云之家集成,提示无法推送企业消息
    k3 cloud中用视图类型来展示数据
    k3 cloud python 插件实现点击对应的单据编号打开单据
    k3 cloud单据转换的表
    sql server查询某个表对应的触发器
    QPainter::begin: Paint device returned engine == 0, type: 3
  • 原文地址:https://www.cnblogs.com/lihanwen/p/12794488.html
Copyright © 2011-2022 走看看