经过GOOGLE发现大多数的解决方法为datetime.ToString(\"s\") 来解决的,经过测试此方法虽然解决的问题,但还不够完美。
因为这样格式化出来的时间在用工具SQLite Developer 查看时显示的时间看起来很怪,不直观。而且如果在SQLite Developer
手动修改了时间,在程序中会报错,因为这个时候保存的时间格式发现了改变。经过测试发现datetime.ToString(\"yyyy-MM-dd
hh:mm:ss\")可以很好的解决这个问题。
二时间的查询
如果你用SQLite作开发,一定少不了时间的查询,一定会让你动不少脑精。因为和别的数据库不一样,就如要查询2009.3.20
11:00:00领取工资的有多少人的SQL怎么写呢,你一定会写成:
select count(*) from T where statue='1' and [date]='2009-03-20 11:00:00'
仔细查看会发现有问题,因为没有结果,实际表中是有结果的,这是为什么,其实我也不没有搞清楚。这个问题还是在国外的一个
论坛发现解决方法的。只要改一下上面的语句就可以了
select count(*) from T where statue='1' and datetime([date])=datetime('2009-03-20 11:00:00') or
select count(*) from T where statue='1' and datetime([date])='2009-03-20 11:00:00'
记住2009-03-20不能写成为2009-3-20.
以上方法经过测目前没有发现问题,当然我也是初次使用SQLite来开发一个小项目,也许还有问题没有发现出来,请各位指教!
SQLite 日期时间函数
SQLite并没有datatime字段类型,但是可以在字符串类型字段中存储时间, 并提供了一些比较实用的日期时间操作函数
strftime(日期时间格式, 日期时间字符串, 修正符, 修正符, ……) strftime( 日期时间格式, 日期时间字符串 ) 也就等价于AAuto中的:
time( 日期时间字符串,日期时间格式 ) ,sqlite与AAuto使用的格式化语法也一样。
参考:http://www.aau.cn/doc/reference/libraries/kernel/time/time.html
strftime() 函数返回一个经过格式化的日期时间, 它可以用下面的符号对日期和时间进行格式化:
%d 一月中的第几天 01-31 %f 小数形式的秒,SS.SSSS %H 小时 00-24
%j 一年中的第几天 01-366 %J Julian Day Numbers %m 月份 01-12 %M 分钟 00-59
%s 从 1970-01-01日开始计算的秒数 %S 秒 00-59
%w 星期,0-6,0是星期天 %W 一年中的第几周 00-53 %Y 年份 0000-9999 %% % 百分号
date,time,datetime,julianday函数
date(日期时间字符串, 修正符, 修正符, ……) 等价于strftime(“%Y-%m-%d”,…) time(日期时间字符串, 修正符, 修正符, ……) 等价于strftime(“%H:%M:%S”,…)
datetime(日期时间字符串, 修正符, 修正符, ……) 等价于strftime(“%Y-%m-%d %H:%M:%S”,…) julianday(日期时间字符串, 修正符, 修正符, ……) 等价于strftime(“%J”,…)
日期时间字符串 可以用以下几种格式:
格式有严格的要求 2008-06-15 03:35:28 日期只能用'-'分隔,时间只能用':' 分隔,不足二位数的必须补零
1.[*]YYYY-MM-DD 2.[*]YYYY-MM-DD HH:MM 3.[*]YYYY-MM-DD HH:MM:SS 4.[*]YYYY-MM-DD HH:MM:SS.SSS 5.[*]YYYY-MM-DDTHH:MM 6.[*]YYYY-MM-DDTHH:MM:SS 7.[*]YYYY-MM-DDTHH:MM:SS.SSS 8.[*]HH:MM 9.[*]HH:MM:SS 10.[*]HH:MM:SS.SSS 11.[*]now
12.[*]DDDD.DDDD
在第五种到第七种格式(ISO8601)中的“T”是一个分割日期和时间的字符; 第八种到第十种格式只代表2000-01-01日的时间,
第十一种格式的‟now‟表示返回一个当前的日期和时间,使用格林威治时间(UTC); 第十二种格式表示一个 Julian Day Numbers。 修正符
日期和时间可以使用下面的修正符来更改日期或时间:
1.[*]NNN days 2.[*]NNN hours 3.[*]NNN minutes 4.[*]NNN.NNNN seconds 5.[*]NNN months 6.[*]NNN years 7.[*]start of month 8.[*]start of year 9.[*]start of week 10.[*]start of day 11.[*]weekday N 12.[*]unixepoch 13.[*]localtime 14.[*]utc
前六个修正符就是简单的增加指定数值的时间和日期;第七到第十个修正符表示返回当前日期的开始;第十一个修正符表示返回下一个星期是N的日期和时间;第十二个修正符表示返回从1970-01-01开始算起的秒数;第十三个修正符表示返回本地时间。
下面举一些例子:
[*]计算机当前时间 SELECT date(„now‟)
[*]计算机当前月份的最后一天
SELECT date(„now‟,‟start of month‟,‟+1 month‟,‟-1 day‟)
[*]计算UNIX 时间戳1092941466表示的日期和时间 SELECT datetime(„1092941466‟,‟unixepoch‟)
[*]计算 UNIX 时间戳1092941466 表示的本地日期和时间 SELECT datetime(„1092941466‟,‟unixepoch‟,‟localtime‟)
[*]计算机当前UNIX 时间戳 SELECT strftime(„%s‟,‟now‟)
[*]两个日期之间相差多少天
SELECT jolianday(„now‟)-jolianday(„1981-12-23‟)
[*]两个日期时间之间相差多少秒
SELECT julianday('now')*86400 - julianday('2004-01-01 02:34:56')*86400
[*]计算今年十月份第一个星期二的日期
SELECT date('now','start of year','+9 months','weekday 2');
[*]取大于现在时间的数据
select * from 表 where 日期字段>datetime('now','localtime')
[*]比较日期指定部份,举一反三,同样使用strftime格式式日期来对日、周、年比较 select * from 表 where strftime('%m',日期字段)=strftime('%m','now') [*]大于指定时间的第一条
select title,pubtime from article where pubtime>'2008-06-15 03:35:28' order by pubtimeasc Limit 1 Offset 0 [*]小于指定时间的第一条
select title,pubtime from article where pubtime<'2008-06-15 03:35:28' order by pubtimedesc Limit 1 Offset 0
简单示例: SELECT
datetime(CHANGE_DATE,'localtime'),
strftime('%Y-%m-%d',CHANGE_DATE,'localtime'), datetime('now','localtime'),
strftime('%Y-%m-%d','now','localtime'), DATE('now','localtime'), time('now','Localtime'),
time('2010-11-27 01:12:21','Localtime','-8 hour') as Time FROM SALARY_HISTORY ;
SELECT * FROM SALARY_HISTORY WHERE date(CHANGE_DATE,'Localtime')=Date('now','Localtime')
Sqlite Working with Dates and Times
In our sample database we have chosen to use integers for columns that store a date value, represented by the format YYYYMMDD. This format is fairly readable and, because the most significant part (the year) comes first, allows arithmetic comparisons to be performed. For instance just as February 29th 2004 is earlier than March 1st, 20040229 is a smaller number than 20040301.This technique is not without its limitations. First, there is no validation on the values stored. Although February 29th is a valid date in the leap year 2004, it does not exist three years out of four and the value 20050229 is not a real date, yet could still be stored in the integer column or compared to a real date.In fact even if you used a trigger to make the number eight digits long and also fall within a sensible year range, there are many values that could still be stored that do not represent dates on the calendar. Very strict checking would be required in your application program to ensure such date information was valid.Similarly, you cannot perform date arithmetic using integer dates. Although 20040101 + 7 gives a date seven days later, 20040330 + 7 would give a number that looks like March 37th.We have not even looked at a data type to store a time value yet, but the same limitations apply if a numeric field is used. SQLite contains a number of functions that allow you to work with both dates and times stored as character strings, allowing you to manipulate the values in useful ways.ValidTimestring Formats SQLite is fairly flexible about the format in which you can specify a date and/or time. The valid time string formats are shown in the following list:
[*]YYYY-MM-DD [*]YYYY-MM-DD HH:MM [*]YYYY-MM-DD HH:MM:SS [*]YYYY-MM-DD HH:MM:SS.SSS [*]HH:MM [*]HH:MM:SS [*]HH:MM:SS.SSS [*]now
[*]DDDD.DDDD
For the format strings that only specify a time, the date is assumed to be 2000-01-01. Where no time is specified, midday is used. Simply using the string now tells SQLite to use the current date and time.The format string DDDD.DDDD represents a Julian day numberthe number of days since noon on November 24, 4714 BC, Greenwich Mean Time. SQLite uses Julian date format internally to manipulate date and time values.Displaying a Formatted Date and Time The core date and time function in SQLite is strftime(), which has the following prototype:strftime(format, timestring, modifier, modifier, ...)
This function is based upon the C function strftime() and the format parameter will accept most, although not all, of the same conversion specifiers. The following example shows how a date can be reformatted to MM/DD/YY format using strftime().sqlite> SELECT strftime('%m/%d/%Y', '2004-10-31');10/31/2004 Table 3.3 lists the conversions that can be performed by SQLite on a timestring. Table 3.3. Date and Time Conversion SpecifiersStringMeaning %dDay of month, 01-31 %fFractional seconds, SS.SSS %HHour, 00-23
%jDay of year, 001-366
%JJulian day number, DDDD.DDDD %mMonth, 00-12 %MMinute, 00-59
%sSeconds since 1970-01-01 (unix epoch) %SSeconds, 00-59
%wDay of week, 0-6 (0 is Sunday) %WWeek of year, 01-53 %YYear, YYYY %%% symbol
Date and Time Modifiers Given one or more optional modifier arguments, strftime() can perform a calculation on the date given in timestring.To add or subtract a period of time, the days, hours, minutes, seconds, months and years modifiers can be used, as shown in these examples:sqlite> SELECT strftime('%Y-%m-%d', '2004-10-31', '+7 days');2004-11-07sqlite>
SELECT
strftime('%H:%M',
'22:00',
'+12
hours');10:00sqlite>
SELECT
strftime('%Y-%m-%d %H:%M:%S','2004-01-01 00:00:00', '-1 second', '+1 year');2004-12-31 23:59:59
NoteThe modifier keywords can be written as either singular or plural. In the last of the preceding examples, we used 1 second and 1 year rather than 1 seconds and 1 years for readability. SQLite does not understand English grammar, so either is always acceptable.
In these examples we have used the same output format as the original timestring to return the date information in a format that can be recognized by SQLite. You should only format the date differently when you want to display it in your application in a particular way.To save having to enter the same format strings repeatedly when working with dates, SQLite provides four convenience functions that call strftime() with predefined formats.Use date() to return a
date with the format string %Y-%m-%d and time() to return a time as %H:%S. The function datetime() returns the date and time using these two formats combined. Finally julianday() uses the %J format specifier to return the Julian day number.The arguments to all four functions are the same as strftime() except that the format argument is omitted. The following example uses datetime() to produce a more concise SQL statement:sqlite> SELECT datetime('2004-01-01 00:00:00', '-1 second', '+1 year');2004-12-31 23:59:59
Other modifiers allow you to adjust a date or time to the nearest significant value. Specifying start of month, start of year, or start of day will decrease the value given in timestring to midnight on the first of the month or year, or on that day respectively.When executed on any day during 2004, the start of year modifier returns 2004-01-01, as shown in the following example:sqlite> SELECT datetime('now', 'start of year');2004-01-01 00:00:00
Modifiers are applied to timestring in the order they appear in the statement, as shown in the following example. Note that had the second statement been executed on the last day of the month, the result would have been differentthe start of the following month would have been returned.sqlite> SELECT datetime('now', 'start of month', '+1 day');2004-07-02 00:00:00sqlite> SELECT datetime('now', '+1 day', 'start of month');2004-07-01 00:00:00 Any number of modifiers can be combined, giving you considerable power when working with dates and times. For instance, the last day of the current month can be found using three modifiers in succession.sqlite> SELECT date('now', '+1 month', 'start of month', '-1 day');2004-07-31
Handling Different Time Zones The locale settings of your system will determine which time zone is used when displaying dates and times; however, the underlying system clock will use Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT)Greenwich Mean Time (GMT). Your time zone setting will specify a number of hours to be added to or subtracted from the UTC value to arrive at the correct local time.For instance, to find the local time in New York you have to subtract five hours from UTC, or four hours during daylight savings time. Even in Greenwich, the local time is UTC + 1 hour during the summer months.To convert between UTC and local time values when formatting a date, use the utc or localtime modifiers. The following examples were run on a system with the timezone set to Eastern Standard Time (UTC 5 hours).sqlite> SELECT time('12:00', 'localtime');2000-01-01 07:00:00sqlite> SELECT time('12:00', 'utc');2000-01-01 17:00:00
SQLite Date And Time Functions http://www.sqlite.org/lang_datefunc.html
Sqlite3时间&函数
Sqlite3支持的数据类型日期函数 Sqlite3 函数2010年05月24日星期一 10:50
Sqlite3支持的数据类型 NULL INTEGER REAL TEXT BLOB
但实际上,sqlite3也接受如下的数据类型: smallint 16 位元的整数。 interger 32 位元的整数。
decimal(p,s) p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值,s是指小数点後有几位数。如果没有特别指定,则系统会设为 p=5; s=0 。 float 32位元的实数。 double 64位元的实数。
char(n) n 长度的字串,n不能超过 254。
varchar(n) 长度不固定且其最大长度为 n 的字串,n不能超过 4000。
graphic(n) 和 char(n) 一样,不过其单位是两个字元 double-bytes, n不能超过127。这个形态是为了支援两个字元长度的字体,例如中文字。
vargraphic(n) 可变长度且其最大长度为 n 的双字元字串,n不能超过 2000 date 包含了 年份、月份、日期。 time 包含了 小时、分钟、秒。
timestamp 包含了 年、月、日、时、分、秒、千分之一秒。
SQLite包含了如下时间/日期函数:
datetime().......................产生日期和时间 date()...........................产生日期 time()...........................产生时间
strftime().......................对以上三个函数产生的日期和时间进行格式化 datetime()的用法是:datetime(日期/时间,修正符,修正符...) date()和time()的语法与datetime()相同。
在时间/日期函数里可以使用如下格式的字符串作为参数: YYYY-MM-DD
YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS HH:MM HH:MM:SS HH:MM:SS.SSS now
其中now是产生现在的时间。
举例(写这个笔记的时间是2006年10月17日晚8点到10点,测试环境:SQLite 2.8.17,WinXP,北京时间):
例1.
select datetime('now'); 结果:2006-10-17 12:55:54 例2.
select datetime('2006-10-17'); 结果:2006-10-17 12:00:00
例3.
select datetime('2006-10-17 00:20:00','+1 hour','-12 minute'); 结果:2006-10-17 01:08:00
例4.
select date('2006-10-17','+1 day','+1 year'); 结果:2007-10-18
例5.
select datetime('now','start of year'); 结果:2006-01-01 00:00:00
例6.
select datetime('now','start of month'); 结果:2006-10-01 00:00:00
例7.
select datetime('now','start of day'); 结果:2006-10-17 00:00:00
例8.
select datetime('now','+10 hour','start of day','+10 hour'); 结果:2006-10-17 10:00:00
例9.
select datetime('now','localtime'); 结果:2006-10-17 21:21:47 例10.
select datetime('now','+8 hour'); 结果:2006-10-17 21:24:45
例3中的+1 hour和-12 minute表示可以在基本时间上(datetime函数的第一个参数)增加或减少一定时间。
例5中的start of year表示一年开始的时间。
从例8可以看出,尽管第2个参数加上了10个小时,但是却被第3个参数“start of day”把时间归零到00:00:00,随后的第4个参数在00:00:00 的基础上把时间增加了10个小时变成了10:00:00。 例9把格林威治时区转换成本地时区。 例10把格林威治时区转换成东八区。
strftime()函数可以把YYYY-MM-DD HH:MM:SS格式的日期字符串转换成其它形式的字符串。 strftime()的语法是strftime(格式, 日期/时间, 修正符, 修正符, ...) 它可以用以下的符号对日期和时间进行格式化: %d 月份, 01-31
%f 小数形式的秒,SS.SSS %H 小时, 00-23
%j 算出某一天是该年的第几天,001-366 %m 月份,00-12 %M 分钟, 00-59
%s 从1970年1月1日到现在的秒数 %S 秒, 00-59
%w 星期, 0-6 (0是星期天)
%W 算出某一天属于该年的第几周, 01-53 %Y 年, YYYY %% 百分号
strftime()的用法举例如下:
例11.
selectstrftime('%Y.%m.%d %H:%M:%S','now','localtime'); 结果:2006.10.17 21:41:09
函数篇:
算术函数
abs(X) 返回给定数字表达式的绝对值。 max(X,Y[,...]) 返回表达式的最大值。 min(X,Y[,...]) 返回表达式的最小值。 random(*) 返回随机数。
round(X[,Y]) 返回数字表达式并四舍五入为指定的长度或精度。 字符处理函数
length(X) 返回给定字符串表达式的字符个数。
lower(X) 将大写字符数据转换为小写字符数据后返回字符表达式。 upper(X) 返回将小写字符数据转换为大写的字符表达式。 substr(X,Y,Z) 返回表达式的一部分。 randstr() quote(A)
like(A,B) 确定给定的字符串是否与指定的模式匹配。 glob(A,B) 条件判断函数
coalesce(X,Y[,...]) ifnull(X,Y) nullif(X,Y) 集合函数
avg(X) 返回组中值的平均值。 count(X) 返回组中项目的数量。 max(X) 返回组中值的最大值。 min(X) 返回组中值的最小值。 sum(X) 返回表达式中所有值的和。 其他函数
typeof(X) 返回数据的类型。
last_insert_rowid() 返回最后插入的数据的ID。 sqlite_version(*) 返回SQLite的版本。
change_count() 返回受上一语句影响的行数。 last_statement_change_count()
管理工具:http://www.sqlitedeveloper.com/ SQLite :http://sqlite.phxsoftware.com/
QT之文件操作
1:定向打开文件,并读取
将要打开的文件导入到你的工程中,具体如何导入,在QT帮助文件welcom界面,点击GetingStarted再选则 >Creating a QT C++ Application 就会有一个实例。 QStringfileName ;
fileName.prepend(\":/\")//在fileName前加字符\" :/"此字符是缺省的相对路径不可少,
如果你导入的文件不止一个,他们放在一个名为files的文件夹内,那么,你就这样设置文件前缀 fileName.prepend(\" :/files/\"),了解了吧。 fileName.append(\".txt\");//在fileName后加字符
总之在设置文件名时,很灵活,可以使用通过信号槽传来的信息数据,也可以自己设制。如fielName = \"Hello\" QFileinPutFile(fileName);//最终inPutFile所绑定的打开一个文件的相对路径字符串。 例如:fileName = \" :/files/first.txt\"
接下来就是打开文件inPutFile.open(QIODevice::ReadOnly);//设为只读形式打开
QTextStream in(&inPutFile);//用QTextStream类对象来读取,因为可以用其中的许多函数 QString line = in.readLine();//一行行的读 QString line = in.readAll();//一下子都读完
Qt之文件操作
分类:Meego2010-12-19 02:141690人阅读评论(0)收藏举报
今天周末,早上起来朋友买了电脑,昨晚特意下了个上网本的meego 1.1,早上起来装了下,界面还行,不过我找了半天都没不知到怎么关机。还好找到了终端,输入shutdown命令关了电脑,后来查了下才发现meego是直接按power键关机的。 今天学习QT的文件操作 1、QIODevice 直接继承自QObject
QIODevice类是输入/输出设备的基类。
QIODevice为设备提供了公共实现和抽象接口用于读写块数据。 QIODevice是一个抽象类,不能被实例化。
被Q3Socket,Q3SocketDevice,QAbstractSocket,QBuffer,QFile,QLocalSocket,QNetworkReply,QProcess继承.
=============================================================================================== 2、QFile
继承自QIODevice
QFile类是一个操作文件的输入/输出设备。
QFile是用来读写二进制文件和文本文件的输入/输出设备。QFile可以自己单独被使用,但是如果和QDataStream或QTextStream一起使用将更加方便。
文件名通常可以通过构造函数来传递,但也可以使用setName()来设置。目录分隔符在任何操作系统下都使用“/\不被支持。你可以通过exists()来检查一个文件是否存在并且可以通过remove()来移去一个文件。更多操作系统相关的高级文件系统操作QT提供了QFileInfo和QDir类.
文件可以用open()来打开、用close()来关闭、用flush()来刷新。数据通常可以使用QDataStream或者QTextStream进行读写,但你也可以使用read(),readLine(),readAll(),write()读写。QFile也支持getChar(),putChar(),和ungetChar()
size()可以返回文件的大小。你可以通过使用pos()函数得到当前文件位置或者使用seek()移到一个新的文件位置。如果你到了文件的末尾,atEnd()返回真。 例1:一行一行读取文件
[c-sharp]view plaincopyprint?
1. #include 5. int main(intargc, char *argv[]) 6. { 7. QCoreApplication a(argc, argv); 8. //中文支持 9. QTextCodec *codec = QTextCodec::codecForName(\"UTF-8\"); 10. QTextCodec::setCodecForCStrings(codec); 11. 12. QFile file(\"/home/administrator/testdir/test.txt\"); 13. if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { 14. qDebug()<<\"Can't open the file!\"< 17. QByteArray line = file.readLine(); 18. QStringstr(line); 19. qDebug()< #include 5. int main(intargc, char *argv[]) 6. { 7. QCoreApplication a(argc, argv); 8. //中文支持 9. QTextCodec *codec = QTextCodec::codecForName(\"UTF-8\"); 10. QTextCodec::setCodecForCStrings(codec); 11. //QTextCodec::setCodecForTr(codec); 12. //QTextCodec::setCodecForLocale(codec); 13. QFile file(\"/home/administrator/testdir/test.txt\"); 14. if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { 15. qDebug()<<\"Can't open the file!\"< 17. QTextStream in(&file); 18. while( !in.atEnd()){ 19. QString line = in.readLine(); 20. qDebug() << line; 21. } 22. returna.exec(); 23. } #include 5. int main(intargc, char *argv[]) 6. { 7. QCoreApplication a(argc, argv); 8. //中文支持 9. QTextCodec *codec = QTextCodec::codecForName(\"UTF-8\"); 10. QTextCodec::setCodecForCStrings(codec); 11. //QTextCodec::setCodecForTr(codec); 12. //QTextCodec::setCodecForLocale(codec); 13. QFile file(\"/home/administrator/testdir/test.txt\"); 14. if(!file.open(QIODevice::ReadWrite | QIODevice::Text)) { 15. qDebug()<<\"Can't open the file!\"< 18. QStringline_in; 19. // while( !stream.atEnd()){ 20. // line_in = stream.readLine(); 21. // qDebug() < 24. stream.seek(file.size());//将当前读取文件指针移动到文件末尾 25. int count = 0; 26. while(count < 10){ 27. stream < #include [c-sharp]view plaincopyprint? 1. #include 4. #include 7. int main(intargc, char *argv[]) 8. { 9. QCoreApplication a(argc, argv); 10. QTextCodec *codec = QTextCodec::codecForName(\"GB2312\"); 11. QTextCodec::setCodecForLocale(codec); 12. QTextCodec::setCodecForCStrings(codec); 13. QTextCodec::setCodecForTr(codec); 14. 15. QDird(\"D:/\"); 16. d.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks | QDir::AllDirs);//列出文件,列出隐藏文件(在 Unix下就是以.开始的为文件),不列出符号链接(不支持符号连接的操作系统会忽略) 17. d.setSorting(QDir::Size | QDir::Reversed);//按文件大小排序,相反的排序顺序 18. constQFileInfoList list = d.entryInfoList();//返回这个目录中所有目录和文件的QFileInfo对象的列表 19. QFileInfoList::const_iterator iterator = list.begin(); 20. qDebug() <<\"目录和文件的数量: \"< 26. qDebug() <<\"当前目录: \"< 34. 35. returna.exec(); 36. } 运行结果: 4、QFileInfo QFileInfo类提供了一个与平台无关的文件信息. QFileInfo提供了关于系统中的文件名称和位置,访问权限和是否是符号链接,文件大小和上次修改/读写时间等,也能被用于Qt资源文件。 QFileInfo能通过相对/绝对路径指向一个文件。 可以使用isFile(),isDir(),isSymLink().symLinkTarget()函数提供符号链接的目标文件。 在Unix(包括Mac OS X)的符号链接具有与目标文件相同的大小(),因为Unix透明地处理符号链接;同样,打开一个符号连接使用的QFile有效地打开链接的目标. 在Windows中,符号链接(快捷方式)的lnk文件。它大小()的是,符号链接(而不是链接的目标)的大小,打开一个符号连接使用的QFile并打开。lnk文件。例如 [cpp]view plaincopyprint? 1. #include 5. int main(intargc, char *argv[]) 6. { 7. QCoreApplication a(argc, argv); 8. QFileInfo info1(\"E:/meego/vi_command.lnk\"); 9. qDebug() << info1.isSymLink(); 10. qDebug() << info1.absoluteFilePath(); 11. qDebug() << info1.size(); 12. qDebug() << info1.symLinkTarget(); 13. qDebug() <<\"**********************\"; 14. QFileInfo info2(info1.symLinkTarget()); 15. qDebug() << info2.isSymLink(); 16. qDebug() << info2.absoluteFilePath(); 17. qDebug() << info2.size(); 18. 19. returna.exec(); 20. } #include [cpp]view plaincopyprint? 1. #include 6. int main(intargc, char *argv[]) 7. { 8. QCoreApplication a(argc, argv); 9. 10. QTextCodec *codec = QTextCodec::codecForName(\"GB2312\"); 11. QTextCodec::setCodecForLocale(codec); 12. QTextCodec::setCodecForCStrings(codec); 13. QTextCodec::setCodecForTr(codec); 14. 15. QFileInfo info(\"E:/meego/基本命令与VI.Command.txt\"); 16. qDebug() < 从零开始,总结经验,一起分享!水平有限,错误之处,敬请指正,不足之处,还望指教。 最近学习了文件相关的操作,特来总结一下。本次主要实现对一个目录下的.TXT文件中的中文用指定字符串替换,并把替换的字符串记录下来。主要是用到了QFile, QFileInfo, QFileDialog, QIODevice(主要是QFile需要使用的相关函数), QTextStream, QDir。另外还接触了两个一些新的类QRegExp(正则表达式),QByteArray(类型相当于char*,功能相当于QString) 建一个基于对话框的Dialog项目,在界面添加三个按钮:目录、文件、保存。在添加一个TextEdit。首先在dialog.h中添加三个函数: 为“文件”按钮添加clicked()的槽函数,在函数中实现打开文件的路径: 添加chReplace函数的定义 添加writeText函数的定义 然后就可以运行测试一下效果了。同样的,文件的读写操作也可以用QTextStream来实现,而且实现起来不需要用到QByteArray类型,只要QString的操作,因此比较起来,两者差不多,我还试了一下两者混合使用也可以。不过用QFile::ReadWrite好像没办法把原来的内容覆盖掉,因此只好分开读和写,另外,可以先把每一行的内容保存在容器之类的里面,写的时候一条一条的写,这种方法方便对要写的内容进行修改,这里就不做介绍了。 为“保存”按钮添加clicked()的槽函数,实现把修改的内容保存为另一个.TXT文件: 当然,你可以根据自己的需要保存相关的内容,也可以不用textEdit先写内容而直接新建一个文件逐条保存。具体实现如下: 用当前时间来命名文件名可以达到不会出现文件名重复的情况。 下面实现查目录,为“目录”按钮添加clicked()的槽函数,实现如下: 添加findFile函数的定义: 这样就可以根据目录来修改文本了。最后需要说明的是[\一-\龥\\\\w]这个正则表达式并不能判断所有的中文,在网上找了很久也没有找到其他的表达式,一般都是[\一-\龥]和[^\\x00-\\xff]这两种,后者是判断双字节的正则表达式,前者是网上公认的。后来不死心啊,在网上找啊找,找啊找,终于找到一位大虾回复了一句有用的话,“1.我是看QRegExp文档说匹配unicode是用\\x,不过实际我试了试\":{"h":18,"w":18,"x":162.03,"y":853.142,"z":25},"ps":null,"t":"word貌似也可以”,于是我也把\":{"h":18,"w":18.03,"x":398.234,"y":853.142,"z":28},"ps":null,"s":{"letter-spacing":"0.03改成\\x,改成[\\\\x4e00-\\\\x9fa5]+,然后测试了一下,果然把之前不能判断的中文也判断进来了,其实帮助文档里也有提示,只是本人既笨眼又拙,未能看出此中的玄机啊。 因篇幅问题不能全部显示,请点此查看更多更全内容