qt实现读写二进制文件

		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();
			}
		}

你可能感兴趣的:(#,C++)