① 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];
}