#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int c = 0;
ifstream infile("c:\\test.txt");
string str;
while (infile>>str)
{
cout<<str<<endl;
++c;
}
string x("hello world, damn it. haha");
stringstream ss(x);
c = 0;
while (ss>>str)
{
cout<<str<<endl;
++c;
}
infile.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int c = 0;
ifstream infile("c:\\test.txt");
string str;
while (infile>>str)
{
cout<<str<<endl;
++c;
}
string x("hello world, damn it. haha");
stringstream ss(x);
c = 0;
while (ss>>str)
{
cout<<str<<endl;
++c;
}
infile.close();
return 0;
}
本文展示了一个使用C++进行文件读取及字符串流操作的简单实例。通过ifstream从本地文件中逐词读取内容,并利用stringstream处理预定义的字符串,演示了如何解析和打印出各个单词。
949

被折叠的 条评论
为什么被折叠?



