① qt文件读取
1、Qt 作为一个通用开发库,提供了跨平台的文件操作能力。文件操作是应用程序必不可少的部分。
2、Qt5增加了QFileDevice类。途中所涉及的类及其用途简要说明如下:
· QFlie:访问本地文件或者嵌入资源;
· QTemporaryFile:创建和访问本地文件系统的临时文件;
· QBuffer:读写QByteArray;
· QProcess:运行外部程序,处理进程间通讯;
· QTcpSocket:TCP协议网络数据传输;
· QUdpSocket:传输 UDP 报文;
· QSslSocket:使用 SSL/TLS 传输数据;
· QFileDevice:新增加的类,提供了有关文件操作的通用实现。
3、这其中,QProcess、QTcpSocket、QUdpSoctet和QSslSocket是顺序访问设备。所谓“顺序访问”,是指它们的数据只能访问一遍:从头走到尾,从第一个字节开始访问,直到最后一个字节,中途不能返回去读取上一个字节;QFile、QTemporaryFile和QBuffer是随机访问设备,可以访问任意位置任意次数,还可以使用QIODevice::seek()函数来重新定位文件访问位置指针。
4、QFile主要提供了有关文件的各种操作,比如打开文件、关闭文件、刷新文件等。我们可以使用QDataStream或QTextStream类来读写文件,也可以使用QIODevice提供的read()、readLine()、readAll()以及write()这样的函数。值得注意的是,有关文件本身的信息,比如文件名、文件所在目录的名字等,则是通过QFileInfo获取,而不是自己分析文件路径字符串。
5、举个例子,打开文件时,需要参数指定打开文件的模式:
Constant Value Description
QIODevice::NotOpen 0x0000 The device is not open.
QIODevice::ReadOnly 0x0001 The device is open for reading.
QIODevice::WriteOnly 0x0002 The device is open for writing.
QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.
QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed.
② 《qt》中如何提取当前目录下指定后缀名的文件
QFileInfoList InfoList = QDir(qApp->applicationDirPath()).entryInfoList();//获取当前目录所有文件
QFileInfoList SuffixInfoList;//定义放提取文件的List
//遍历
foreach(QFileInfo fileInfo, QFileInfoList)
{
if(!fileInfo.isFile()) continue;//不是文件继续,只用于加速,可不加
//后缀不区分大小写,需要区分直接用“==”
if(0==fileInfo.suffix().compare(后缀), Qt::Caseinsensitive)
{
SuffixInfoList << fileInfo;//指定后缀,加入列表
}
}
以上纯为手打,不是Copy的,可能有些拼写错误大小写错误什么的,望谅解
③ 在Qt中怎样获得一个文件的所在的路径
在上传前你要往用户的cookie里放个标识,接收到上传文件后,也就是第一个请求,里面有文件名什么的基本信息,其中有个参数会告诉你一共有几个包,然后你把后续收到的该用户标识的几个包合并,还原成第一个包收到的文件名,这时候分片上传结束。
④ QT编程 获取当前工作目录下的文件列表,并以Tree的形式显示在界面上
String[] s={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
System.out.print("请输入数字(1-12):");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int m=Integer.parseInt(str);
if (m<=0||m>=13)
{
⑤ QT读取文本文件
据我所知,QTextString这个类貌似是不存在的。倒是有个QTextStream类可以用来读取文本数据。
例如下面的函数内可以读取小文本文件中的内容容
void MainWindow::readFile(const QString& fpath)
{
if(fpath.isEmpty())
return;
QFile file(fpath);
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
{
qDebug()<<"Open file failure!";
return;
}
QTextStream fs(&file);
QString fileContent (fs.readAll());
qDebug()<<"The content of file is \n"<<fileContent;
}
你需要确保你将正确的文件路径传递给了该函数,否则会导致打开文件失败。
⑥ qt 怎样获取文件夹下的所有文件
QCoreApplication有几个静态方法可以实现,我一般用QCoreApplication::applicationDirPath()获取可执行文件得目录
⑦ qt中连接到ftp服务器上后怎么获取服务下所有的文件名
操作方法如下:
@echo off
set h=192.168.1.100
set u=ftpuser
set p=12345678
echo open %h%>ftp.txt
echo %u%>>ftp.txt
echo %p%>>ftp.txt
echo dir>>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt>ftpdir.txt
echo open %h%>ftp.txt
echo %u%>>ftp.txt
echo %p%>>ftp.txt
for /f "tokens=4" %%i in ('findstr "<DIR>" ftpdir.txt') do (
echo cd %%~i>>ftp.txt
echo dir>>ftp.txt
echo cd ..>>ftp.txt)
echo bye>>ftp.txt
ftp -s:ftp.txt>ftpfile.txt
notepad ftpfile.txt
⑧ Qt如何读取数据文件
#include <QFile>
#include <QString>
#include <QIODevice>
#include <QDebug>
#include <QTextStream>
void Read(QString Filename)//名字自己定,需要带路径
{
QFile mFile(Filename);
if(!mFile.open(QFile::ReadOnly|QFile::Text))
{
qDebug()<<"could not open file for read!";
return;
}
QTextStream in(&mFile);
QString mtext = in.readAll();
// mtext = mtext.trimmed();
qDebug()<<mtext;
if(mtext == "")
qDebug()<<" read over!";
mFile.flush();
mFile.close();
}
⑨ qt如何得到对话框文件路径
if(fileDialog->exec() == QDialog::Accepted) {
QString path = fileDialog->selectedFiles()[0];
}