Qt讀取當前專案下的檔案

2021-10-06 08:19:18 字數 953 閱讀 4686

一、就是我們自己的專案下有乙個data.txt的檔案和data的資料夾裡面有乙個datastream.txt的檔案,我分別想直接讀取裡面的檔案

1、需要加上標頭檔案如下:

#include #include #include
2、讀取data.txt檔案如下,直接跟mainwindow放在同一層目錄:

qstring filename ="data.txt";

qfile file(filename);

if (!file.open(qiodevice::readonly | qiodevice::text))

return;

qtextstream txtinput(&file);

txtinput.setcodec("utf-8");

qstring str ;

while (!txtinput.atend())

qdebug()<<"str ="<2、直接在mainwindow的同級目錄下新建乙個資料夾data,然後裡面放上datastream.txt檔案,操作如下:

//第二種做法,比較安全點

qstring path = qdir::currentpath();

qstring filename =path+"/data/datastream.txt";

qfile file(filename);

if (!file.open(qiodevice::readonly | qiodevice::text))

return;

qtextstream txtinput(&file);

txtinput.setcodec("utf-8");

qstring str ;

while (!txtinput.atend())

qdebug()<<"str ="<

Qt專案下pro檔案設定庫路徑

正常情況下我們會將工程的路徑用乙個變數來儲存 例如 然後後面的變數引用這個變數,例如 includepath thirdparty sqlite3 include libs lrt lpthread l thirdparty sqlite3 lib lsqlite3 lsqlite3 json但是在...

當前專案的品牌列表的查詢

service public inte ce brandserviceserviceimp service public class brandserviceimpl implements brandservice brandcontroller restcontroller public clas...

Java專案下exe檔案的路徑問題及執行

如圖 exe檔案路徑獲取 首先獲取當前專案的根目錄,然後得到mibbrowser.exe檔案的路徑 如下 file directory new file 引數為空 string coursefile directory.getcanonicalpath private static final st...