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]

  • 相关阅读:
    敏捷开发学习笔记-Agile development(AM)
    WindowsServer --------- 在服务器中安装sqlserver 数据库
    WIndowsServer ---------- 将本地文件映射到服务器
    windowsServer-------- 系统中调出文件扩展名
    SQLServer -------- 解决忘记sa 密码,创建一个新的
    SQLServer ---------- 附加数据库,以及解决附加时出现错误
    AJAX -------------- 如何使用ajax
    网络基础 ----------- osi 与 一些协议
    装系统---------- u盘 安装系统
    网络基础 ----------- 电脑设置为wifi站点
  • 原文地址:https://www.cnblogs.com/lihanwen/p/12794488.html
Copyright © 2011-2022 走看看