仅做测试用!
按行读入
- 只读取一行
/*************************************************************************
> 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;
}
测试结果:
- 按行读取整个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;
}
如果是按字符读入,需要将输出为char
类型即可
按字符读入,结果需要什么类型,输出类型就是什么
参考:
[c++读取txt][https://www.cnblogs.com/VVingerfly/p/4435898.html]