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]

原文地址:https://www.cnblogs.com/lihanwen/p/12794488.html