如何用C++读取文本文件,急!

来源:百度知道 编辑:UC知道 时间:2024/07/06 15:42:32
如何用C++读取下面文件
2.txt

10/12/2008,27:57:43,819,4302,2366,47,964,153,17.3,0,Manual,53
10/12/2008,17:57:03,864,4593,2619,57,1053,169,17.4,0,Manual,52
10/12/2008,17:56:11,899,4836,2853,70,1144,187,17.4,0,Manual,51
10/12/2008,17:55:45,910,4915,2932,74,1177,193,17.4,0,Manual,50
10/12/2008,17:55:08,928,5016,3038,82,1222,202,17.4,0,Manual,49
10/12/2008,17:54:29,944,5127,3144,90,1267,211,17.4,0,Manual,48

请教高手,谢谢!

假设你的2.txt位于D盘

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main()
{
ifstream ifs;
ifs.open("D:\\2.txt");

string str, buf;
while(!ifs.eof())
{
ifs>>buf;
str += buf + '\n';
}
cout<<str<<endl;
}