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]

  • 相关阅读:
    一个Spring的应用看起来象什么?
    IOC的优点是什么?
    解释对象/关系映射集成模块?
    XMLBeanFactory?
    使用Spring框架的好处是什么?
    什么是Spring MVC框架的控制器?
    什么是Spring的MVC框架?
    在Spring AOP 中,关注点和横切关注的区别是什么?
    你更倾向用那种事务管理类型?
    Spring支持的事务管理类型?
  • 原文地址:https://www.cnblogs.com/lihanwen/p/12794488.html
Copyright © 2011-2022 走看看