C++文件读取的四种方式

C++可以根据不同的目的来选取文件的读取方式,目前为止学习了C++中的四种文件读取方式。

C++文件读取的一般步骤:

1、包含头文件 #include

2、创建流对象:ifstream ifs(这里的ifs是自己起的流对象名字)

3、打开文件:file.open("文件路径","打开方式"),打开文件后并判断文件是否打开成功,ifs.is_open()是用于判断文件是否打开的语句

4、进行文件读取操作

5、关闭文件

ifs.close();

第一种方法:采用“<<”运算符

#include
#include
using namespace std;
 
int main()
{
 // 创建流对象	
ifstream ifs;
 //打开文件并判断是否打开成功如果成功则读取数据
	readFile.open("test.txt", ios::in);
	if (ifs.is_open())
	{
		cout << "文件打开成功" << endl;
		//第一种方法
        char buf[100] = { 0 };
		while (ifs >>buf)
		{
			cout << buf << endl;
		}
	}
	else
	{
		cout << "文件打开失败" << endl;
	}
    //关闭文件
	ifs.close();
 
 
	return 0;
}

或者可以写成以下,将判断是否成功打开文件换一种方式实现读取数据


#include
#include
using namespace std;

void test01()
{
	// 2创建流文件
	ifstream ifs;
	// 3打开文件并判断是否打开成功
	ifs.open("test.txt", ios::in);
		if (!ifs.is_open())
		{
			cout << "打开失败" << endl;
			
		}
		//读数据第一种方法
		char buff[1024] = {0};
		while (ifs >> buf)
		{
			cout << buf << endl;
		}

解释:这种方式和采用cin>>进行写入类型,ifs>>buf进行写入时永远是从buff[0]开始,不会进行追加式的写入,ifs在遇到EOF(End Of File)时会返回False,以此来跳出while循环。

局限性:由于“<<”运算符自身的性质,会将一切空格当作终止条件,这样的话就无法输出空格信息,采用这种方法会将一行信息进行分段读出,对于类对象的信息读取是很有用的
 

 第二种方法:利用中的getline函数,按行读取,这样每一行内的所有空格也可以顺利读取

#include
#include
#include
using namespace std;
 
int main()
{
	ifstream ifs;
	ifs.open("test.txt", ios::in);
 
	if (readFile.is_open())
	{
		cout << "文件打开成功" << endl;
		
		string buf;
		while (getline(ifs,buf))
		{
			cout << buf << endl;
		}
	}
	else
	{
		cout << "文件打开失败" << endl;
	}
 //关闭文件   
 ifs.close();
  
	return 0;
}

同样,也有两种方式

#include
#include
using namespace std;

void test01()
{
	// 2创建流文件
	ifstream ifs;
	// 3打开文件并判断是否打开成功
	ifs.open("test.txt", ios::in);
		if (!ifs.is_open())
		{
			cout << "打开失败" << endl;
			
		}
      //第二种
		string buf;
		while(getline(ifs,buf))
		{
			cout << buf << endl;
		}
    //5 关闭文件
		ifs.close();
}

getline()函数会在文件读完时返回False跳出while循环,getline可以一次读取一行文件,保持读出内容和文件内容一致。

第三种方法: 利用ifs流对象内部的getline方法,这个名字虽然和第二种中的一样,但是传入的参数不同,并不是同一个函数

#include
#include
#include
using namespace std;
 
int main()
{
//创建流对象	
ifstream ifs;
	readFile.open("test.txt", ios::in);
 
    //判断文件是否成功打开成功则读取数据
	if (ifs.is_open())
	{
		cout << "文件打开成功" << endl;
		char buf[100] = { 0 };
		while (ifs.getline(buf,sizeof(buf)))
	    {
		    cout << buf << endl;
        }
		
	}
	else
	{
		cout << "文件打开失败" << endl;
	}
 
 
     //关闭文件
	ifs.close();
	return 0;
}
 
#include
#include
using namespace std;

void test01()
{
	// 2创建流文件
	ifstream ifs;
	// 3打开文件并判断是否打开成功
	ifs.open("test.txt", ios::in);
		if (!ifs.is_open())
		{
			cout << "打开失败" << endl;
			
		}
        //读取数据的第三种方法
		char buf[100] = { 0 };
		while (ifs.getline(buf, sizeof(buf)))
		{
			cout << buf << endl;
		}
    //5 关闭文件
		ifs.close();
}

 第四种方法:利用ifs内部的get()函数一个个的将字符读出来,遇到EOF时返回False退出while循环,EOF 是 end of file的标志,不太建议使用这个,因为这个很慢,只能一个一个字符读取

#include
#include
using namespace std;
 
int main()
{
    //创建流对象
	ifstream ifs;
	ifs.open("test.txt", ios::in);
	//判断是否成功打开如果打开则读取数据
	if (ifs.is_open())
	{
		cout << "文件打开成功" << endl;
		char buf = 0;
		while ((buf=ifs.get())!=EOF)
		{
			cout << buf;
		}
	}
	else
	{
		cout << "文件打开失败" << endl;
	}
//关闭文件	
ifs.close();
	return 0;
}
 
#include
#include
using namespace std;

void test01()
{
	// 2创建流文件
	ifstream ifs;
	// 3打开文件并判断是否打开成功
	ifs.open("text.txt", ios::in);
		if (!ifs.is_open())
		{
			cout << "打开失败" << endl;
			
		}
	// 第四种读取方法
		char c;
		while(((c = ifs.get()) != EOF))
		{
			cout << c;
		
		}
		//5 关闭文件
		ifs.close();
}

你可能感兴趣的:(c++,开发语言,c语言)