1. 程式人生 > >QDataStream讀寫檔案

QDataStream讀寫檔案

#include <qfile.h>
#include <qdebug.h>

Qt_text_stream::Qt_text_stream(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	write_data();
	read_data();
}


//寫檔案
void Qt_text_stream::write_data()
{
	//建立一個檔案直接命名  或者  p_file.setFileName("")
	QFile p_file("q_data_stream.txt");
	//開啟
	if (true == p_file.open(QIODevice::WriteOnly))
	{
		//QTextStream 關聯檔案
		QDataStream data_stram(&p_file);
		
		//二進位制編碼
		//寫入檔案
		data_stram << QString("000") << 999;
		//關閉檔案
		p_file.close();
	}

}

void Qt_text_stream::read_data()
{
	//建立一個檔案直接命名  或者  p_file.setFileName("")
	QFile p_file("q_data_stream.txt");
	//開啟
	if (true == p_file.open(QIODevice::ReadOnly))
	{
		//QTextStream 關聯檔案
		QDataStream data_stram(&p_file);
		
		QString str;
		int a;
		data_stram >> str >> a;

		qDebug() << str <<"---str----"<< a;

		//關閉檔案
		p_file.close();
	}



}