1,按行读取
// TEST_Read.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>//读取文件所需的文件头
//以下为读取文件的一种方法:将文件每行内容存储到字符串中,再输出字符串
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char buffer[10];
fstream out;
out.open("test.txt",ios::in);
cout<<"test的内容如下:"<<endl;
int i=0;
while(!out.eof())//判断是否读到文件的末尾.
{
out.getline(buffer,10,'\n'); //表示该行字符达到10个或遇到换行就结束
cout<<buffer<<endl;
}
out.close();
cin.get();//读取最后一个enter键.
return 0;
}
// TEST_Read.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>//读取文件所需的文件头
//直接读取txt文件中的数据,其中以txt中的空格符隔开
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int buffer[10];
fstream out;
out.open("test.txt",ios::in);
cout<<"test的内容如下:"<<endl;
for(int i=0;i<10;i++)
out>>buffer[i];
for(int i=0;i<10;i++)
cout<<buffer[i]<<endl;
out.close();
}