标签:style blog http io os ar for sp 文件
第一个例子,我们采用STL的方式:
#include <iostream> int main() { std::cout << "console application\n"; }
第二个例子我们用QT4编程库
#include <QTextStream> int main() { QTextStream out(stdout); out << "console application\n"; }
Output
console application
写一行字符串到文件中
#include <QTextStream> #include <QFile> int main() { QFile data("myfile"); if (data.open(QFile::WriteOnly)) { QTextStream out(&data); out << "You make me want to be a better man." << endl; } }
$ cat myfile You make me want to be a better man.
下面的例子我们输出一段文本到控制台
S a régi szeret?mér mit nem cselekednék, tengerb?l a vizet kanállal lemerném. S a tenger fenekér?l apró gyöngyöt szednék, s a régi szeret?mnek gyöngykoszorút kötnék.
#include <QTextStream> #include <QFile> int main() { QFile data("szerelem"); QString line; if (data.open(QFile::ReadOnly)) { QTextStream in(&data); QTextStream out(stdout); out.setCodec("UTF-8"); in.setCodec("UTF-8"); do { line = in.readLine(); out << line << endl; } while (!line.isNull()); } }
S a régi szeret?mér mit nem cselekednék, tengerb?l a vizet kanállal lemerném. S a tenger fenekér?l apró gyöngyöt szednék, s a régi szeret?mnek gyöngykoszorút kötnék.
Qt容器类之一
#include <QTextStream> #include <QList> int main() { QTextStream out(stdout); QList<QString> list; list << "Balzac" << "Tolstoy" << "Guldbrassen" << "London" << "Galsworthy" << "Sienkiewicz"; qSort(list); for (int i = 0; i < list.size(); ++i) { out << list.at(i) << endl; } }
Output
Balzac Galsworthy Guldbrassen London Sienkiewicz Tolstoy
管理文件目录
#include <QTextStream> #include <QDir> int main() { QTextStream out(stdout); QString home = QDir::homePath(); out << home << endl; }
Output
/home/vronskij
输出应用程序所在路径中扩展名是.c的全部文件名字
#include <QTextStream> #include <QDir> int main() { QTextStream out(stdout); QDir dir; QStringList filters; filters << "*.c" << "*.c~"; dir.setNameFilters(filters); QFileInfoList list = dir.entryInfoList(); for (int i = 0; i < list.size(); ++i) { QFileInfo fileInfo = list.at(i); out << QString("%1").arg(fileInfo.fileName()); out << endl; } }
$ ls -F anim* anim.c anim.c~ filters* $ ./filters anim.c anim.c~
输出当前时间
#include <QTextStream> #include <QTime> int main() { QTextStream out(stdout); QTime qtime = QTime::currentTime(); QString stime = qtime.toString(Qt::LocalDate); out << stime << endl; }
Output
$ ./time 10:30:33 PM
字符串连接
#include <QTextStream> int main() { QString a = "Disziplin "; QString b = "ist "; QString c = "Macht.\n"; QTextStream out(stdout); out << a + b + c; }
$ ./concat Disziplin ist Macht.
字符串追加
#include <QTextStream> int main() { QString string = "Whether I shall "; string.append("turn out to be the hero of my own life, \n"); string.append("or whether that station will be held by anybody else, \n"); string.append("these pages must show.\n"); QTextStream out(stdout); out << string; }
$ ./append Whether I shall turn out to be the hero of my own life, or whether that station will be held by anybody else, these pages must show.
参数替换
#include <QTextStream> int main() { QString string = "What if I gave you %1 red roses?"; int num = 21; QTextStream out(stdout); out << string.arg(num) << endl; }
$ ./str3 What if I gave you 21 red roses?
输出字符串长度
#include <QTextStream> int main() { QString string = "The history of my life."; QTextStream out(stdout); out << "The string has " + QString::number(string.size()) + " characters." << endl; }
./size The string has 23 characters.
字符串大小写转换
#include <QTextStream> int main() { QString string = "The history of my life."; QTextStream out(stdout); out << string.toLower() << endl; out << string.toUpper() << endl; }
$ ./uplow the history of my life. THE HISTORY OF MY LIFE.
标签:style blog http io os ar for sp 文件
原文地址:http://www.cnblogs.com/xchsp/p/4063316.html