欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

qt实现读写二进制文件

程序员文章站 2022-04-22 09:47:19
QFile file("D:\\Tcpview.exe");if (file.exists()){if ( file.size() > 2097152 ){QMessageBox::information(this, tr("up file fail"), tr("Failed to up file, file size need < 2M."));return;}if(!file.open(QIODevice::R.....
		QFile file("D:\\Tcpview.exe");
		if (file.exists())
		{
			if ( file.size() > 2097152 )
			{
				QMessageBox::information(this, tr("up file fail"), 
					tr("Failed to up file, file size need < 2M."));
				return;
			}
			if(!file.open(QIODevice::ReadOnly))
			{
				QMessageBox::warning(this,tr("error"),tr("file no exit"),QMessageBox::Ok);
				return;
			}
			else
			{
				////////////////////////////////////
				//fstream fin;
				ifstream fin("D:\\Tcpview.exe", ios::in | ios::binary);
				//fin.open(filename2.c_str());
				if(!fin)
				{
					cerr<<"open error!"<<endl;
					return;
				}
				int i = file.size();
				QByteArray str = file.read(i);  //写
				//注意:不可使用string str = file.read(i);  //会被截断

				QFile file2("D:\\Tcpview3.exe");
				file2.open(QIODevice::WriteOnly);
				file2.write(str.data(), i);
				file2.close();
			}
		}

本文地址:https://blog.csdn.net/qq_33053671/article/details/107388240

相关标签: # C++