#include
#include
#include
using namespace std;
int main()
{
ifstream ifs("test.cpp"); // 改成你要打开的文件
streambuf* old_buffer = cin.rdbuf(ifs.rdbuf());
string read;
while(cin >> read) // 逐词读取方法一
cout << read;
cin.rdbuf(old_buffer); // 修复buffer
}
#include
#include
using namespace std;
int main()
{
ifstream ifs("test.cpp"); // 改成你要打开的文件
ifs.unsetf(ios_base::skipws);
char c;
while(ifs.get(c)) // 逐词读取方法二
{
if(c == ' ')
continue;
else
cout.put(c);
}
}
#include
#include
#include
using namespace std;
int main()
{
ifstream ifs("test.cpp"); // 改成你要打开的文件
string read;
while(getline(ifs, read, ' ')) // 逐词读取方法三
{
cout << read << endl;
}
}
#include
#include
using namespace std;
int main()
{
ifstream ifs("test.cpp"); // 改成你要打开的文件
char buffer[256];
while(ifs.getline(buffer, 256, ' ')) // 逐词读取方法四
{
cout << buffer;
}
}
可以用string来读取中文
#include"fstream"
#include"string"
using namespace std;
int main()
{
string s;
ifstream fin;
ofstream fout;
fin.open("F:\\1.txt");
fout.open("F:\\1.xls");
while(!fin.eof())
{
getline(fin,s); //读取一行字符,要求一行字符中没有制表符'\t',否则用本程序实现不了把一行字符输出到一格excle中
fout< }
fin.close();
fout.close();
system("pause");
return 0;
}
{
FILE *fp;
char buf[1000];
fp=fopen("student.txt","r");
if(!fp)
{
printf("打开文件出错\n");
return;
}
while(fscanf(fp,"%s",buf))>0)
{
..................................
}
fclose(fp);
}
这样就能每次读取一行字符串
试试将txt文件另存为unicode编码。。。
vs2008 、vs2005下 std::ifstream中不支持中文,是因为在vs2008 、vs2005 下,默认传入的 unicode 字符集 ,而一般开发的时候 大多数使用的多字节字符集,所以会导致出错 。
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL, "Chinese-simplified");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
3. 或者 直接 修改 log4cpp 的 参数,直接传入的是 宽字节 。