1. #include <QCoreApplication>
  2. #include <QMap>
  3. #include <QFile>
  4. #include <QDir>
  5. #include <QDebug>
  6. #include <QTextStream>
  7. #include <QDataStream>
  8. #include <QLinkedList>
  9. void qDirTest()
  10. {
  11.  
  12. QDir dir("F:/NVIDIA");
  13. qDebug() << dir.exists() ;
  14. qDebug() << dir.absolutePath();
  15. dir.cdUp();
  16. qDebug() << dir.absolutePath() << "\n-------";
  17.  
  18. dir.cd("NVIDIA");
  19. qDebug() << dir.dirName();
  20.  
  21. QString path = "E:/test_dir";
  22. QDir dir_test(path);
  23. if(!dir_test.exists())
  24. {
  25. dir_test.mkdir(path);
  26. }
  27. dir_test.rmdir("E:/test_dir");
  28.  
  29. // Show The Drive of files
  30. QString dir_path = "E:/";
  31. QDir dir_list(dir_path);
  32. foreach( QFileInfo item,dir_list.entryInfoList())
  33. {
  34. if(item.isFile())
  35. {
  36. qDebug() << item.absoluteFilePath() <<" --->is File";
  37. }
  38. if(item.isDir())
  39. {
  40. qDebug() << item.absoluteFilePath() <<" --->is Dir";
  41. }
  42. }
  43.  
  44. }
  45.  
  46. // QFile TEST
  47.  
  48. namespace QFileTextDataStream
  49. {
  50. void write_text_stream(QString name)
  51. {
  52. QFile wFile(name);
  53. if(!wFile.open(QFile::WriteOnly | QFile::Text)) // IT's FILE *
  54. {
  55. qDebug () << "open files error";
  56. }
  57.  
  58. QTextStream stream(&wFile);
  59. stream << "Hello World\n" << "This is jack";
  60.  
  61. wFile.flush();
  62. wFile.close();
  63. }
  64. void read_text_stream(QString name)
  65. {
  66.  
  67. QFile rFile(name);
  68. if(!rFile.open(QFile::ReadOnly | QFile::Text)) // IT's FILE *
  69. {
  70. qDebug () << "open files error";
  71. }
  72. QTextStream in(&rFile);
  73. QString data = in.readAll();
  74. qDebug() << data;
  75.  
  76. }
  77.  
  78. void QFileTextTest(QString FileName)
  79. {
  80. qDebug () << "write file test";
  81. write_text_stream(FileName);
  82. qDebug() << "now Reading";
  83. read_text_stream(FileName);
  84. }
  85.  
  86. void write_binary_stream(QString name)
  87. {
  88. QFile file(name);
  89. if(!file.open(QIODevice::WriteOnly))
  90. {
  91. qDebug () << "error";
  92. }
  93. QDataStream out(&file);
  94. out << QString("Houdini ") << ;
  95. file.flush();
  96. file.close();
  97. }
  98. void read_binary_stream(QString name)
  99. {
  100. QFile file(name);
  101. if(!file.open(QIODevice::ReadOnly))
  102. {
  103. qDebug () << "error";
  104. }
  105. QDataStream read(&file);
  106. QString fname;
  107. int val;
  108. read >> fname >> val;
  109. qDebug() << fname<<":" << val;
  110. file.flush();
  111. file.close();
  112. }
  113.  
  114. }
  115.  
  116. namespace DataCore
  117. {
  118. void data_struction_test()
  119. {
  120. QMap<int ,QString> map;
  121. map.insert(,"Houdini");
  122. map.insert(,"nuke");
  123. map.insert(,"maya");
  124. foreach (int i,map.keys())
  125. {
  126. qDebug() << map[i] ;
  127. }
  128.  
  129. }
  130.  
  131. }
  132. int main(int argc, char *argv[])
  133. {
  134. QCoreApplication a(argc, argv);
  135. //DirTest();
  136. //QFileTextDataStream::QFileTest("F:/test.txt");
  137. QFileTextDataStream::write_binary_stream("F:/test.binary");
  138. QFileTextDataStream::read_binary_stream("F:/test.binary");
  139. //DataCore::data_struction_test();
  140. return a.exec();
  141. }

QTextStream QDataStream

CopyFile Method 1:

  1. // QFile And QDataStream
  2. int main(int argc, char *argv[])
  3. {
  4. QCoreApplication a(argc, argv);
  5. QFile file("F:/dj.mp3");
  6. if(!file.open(QFile::ReadOnly)){
  7. perror("error open\n");
  8. return ;
  9. }
  10.  
  11. QFile copy_file("F:/copytest.mp3");
  12. if(!copy_file.open(QFile::WriteOnly)){
  13. perror("error write open\n");
  14. return ;
  15. }
  16.  
  17. QDataStream from_data(&file);
  18. QDataStream to_data(&copy_file);
  19.  
  20. int buffer_size = ;
  21. char data[buffer_size];
  22. while(!file.atEnd())
  23. {
  24. from_data.readRawData(data,buffer_size);
  25. to_data.writeRawData(data,buffer_size);
  26. }
  27.  
  28. file.close();
  29. copy_file.close();
  30. fprintf(stdout,"%s \n","end reading");
  31. return a.exec();
  32. }

CopyFile Method 2:

  1. QCoreApplication a(argc, argv);
  2. QFile file("F:/houdini-13.0.509.exe");
  3. if(!file.open(QFile::ReadOnly))
  4. {
  5. perror("error open\n");
  6. return ;
  7. }
  8.  
  9. QFile copy_file("F:/copytest.exe");
  10. if(!copy_file.open(QFile::WriteOnly))
  11. {
  12. perror("error write open\n");
  13. return ;
  14. }
  15.  
  16. QByteArray line;
  17. while(!file.atEnd())
  18. {
  19. line.clear();
  20. line =file.read();
  21. copy_file.write(line);
  22. }
  23.  
  24. fprintf(stdout,"%s \n","end reading");
  25. file.close();
  26. copy_file.close();
  27. return a.exec();

DataStream And ByteArray

  1. #include <QCoreApplication>
  2. #include <QDataStream>
  3. #include <QByteArray>
  4. #include <QDebug>
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication a(argc, argv);
  8. QByteArray block;
  9. QDataStream stream(&block,QIODevice::WriteOnly);
  10. stream<<quint16() << QString("Houdini")<<QString("Maya");
  11. stream.device()->seek();
  12. stream<<quint16();
  13. qDebug() << block;
  14.  
  15. // read back
  16. QDataStream stream_read(&block,QIODevice::ReadOnly);
  17. quint16 val;
  18. QString name;
  19. QString func;
  20. stream_read>>val >>name >>func;
  21.  
  22. qDebug() << val << name <<func;
  23.  
  24. return a.exec();
  25. }

分割文件流:)

  1. void get_buffer_split2(QByteArray readInArray,QList<QByteArray> &data,int buffer_size)
  2. {
  3.  
  4. float t = buffer_size;
  5. int num = ceil(float(readInArray.size())/float(t));
  6.  
  7. for(int i=;i<=num;i++)
  8. {
  9. QByteArray _temp = readInArray.left(buffer_size);
  10. readInArray.remove(,buffer_size);
  11. data.push_back(_temp);
  12. }
  13. }
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. QCoreApplication a(argc, argv);
  18. QByteArray readArray;
  19. QFile readFile("/home/gearslogy/icon.png");
  20. if(!readFile.open(QFile::ReadOnly))
  21. {
  22. qDebug() << "error read file";
  23. readFile.close();
  24. return ;
  25. }
  26. readArray = readFile.readAll();
  27.  
  28. QList<QByteArray> split_data;
  29. get_buffer_split2(readArray,split_data,);
  30.  
  31. QFile writeFile("/home/gearslogy/iconCopy.png");
  32. if(!writeFile.open(QFile::WriteOnly))
  33. {
  34. qDebug() << "write file error";
  35. writeFile.close();
  36. return ;
  37. }
  38.  
  39. for(int i=;i<split_data.size();i++)
  40. {
  41. QByteArray _temp = split_data[i];
  42. writeFile.write(_temp);
  43. }
  44. writeFile.close();
  45.  
  46. return a.exec();
  47. }

QFile QDataStream QTextStream的更多相关文章

  1. 4.关于QT中的QFile文件操作,QBuffer,Label上添加QPixmap,QByteArray和QString之间的区别,QTextStream和QDataStream的区别,QT内存映射(

     新建项目13IO 13IO.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets network CON ...

  2. (十四)QFile操作,QByteArray,文件流操作,QTextStream,QDataStream,QFileInfo, QIODevice

    QFile f 1.readall #include "widget.h" #include "ui_widget.h" #include <QFileD ...

  3. QT文件(夹)操作---QFile、QDir、QFileInfo、QTextStream和QDataStream异同

    1.1    文件和目录 QFile.QBuffer和QTcpSocket可支持读写设备,用open函数打开,用write或putChar函数写入.用read和readLine或readAll进行读取 ...

  4. Qt笔记——QFile,QDataStream,QTextStream

    QFile #ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } clas ...

  5. 4.关于QT中的QFile文件操作,QBuffer,Label上加入QPixmap,QByteArray和QString之间的差别,QTextStream和QDataStream的差别,QT内存映射(

     新建项目13IO 13IO.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets network CON ...

  6. 使用QFile进行文件操作(QFile可以使用FILE *指针,还必须指定AutoCloseHandle)

    QFile类我我们提供了操作文件的常用功能.它是一种io设备,可以用来读写文本文件和二进制文件,也可以用来读写Qt的资源文件.QFile类可以单独使用,该类本身提供了read/write函数,但更方便 ...

  7. Qt:QFile、QIODevice

    QFile 0.说明 QFile是读写文件的类,这里的文件包括文本文件.二进制文件.资源文件. 通常情况下,文件读写使用QFile.QTextStream.QDataStream就够了. file n ...

  8. Qt: 把内容写进字符串中与C++很相似(使用QTextStream包装QString)

    #include <iostream>#include <QChar>#include <QFile>#include <QTextStream>#in ...

  9. Qt:QTextStream

    0.说明 QTextStream提供了读写文本文件的接口. QTextStream可以操作QIODevice,  QByteArray 和 QString,调用QTextStream的流操作可以方便的 ...

随机推荐

  1. 引用log4j.jar包后,出现告警

    问题现象:在引用log4j包后,使用自己导出的jar包,编译测试例代码,在启动浏览器时出现以下告警:log4j:WARN No appenders could be found for logger ...

  2. 在SQL语句中加入时间比较作为查询条件

    select * from 表名 where 列名 = ? and DATEDIFF(hh,时间列,'2016-08-22 15:05:59.000')<9

  3. svn中第一次check out working copy项目下来出现 ld: library not found for -lcrypto clang: error: linker command failed with exit code 1 (use -v to see invocation)

    这个问题主要是.a文件的忽略删除,需要更改设置,并且把文件重新添加

  4. [java]删除数组中的某一个元素

    package org.company.project.test; import java.util.Arrays; import java.util.Scanner; public class Ar ...

  5. Git & Gitlab 使用指南

    2016-02-23   |   9,129字   |   分类于 工具  |   3条评论 去年小组在从 SVN 和 TFS 迁移到 Git 的过程中整理了这份文档,面向的用户是对 Git 和 SV ...

  6. as3绕过策略文件给视频截图

    接上篇 http://www.cnblogs.com/DarkMaster/p/5973593.html 这篇同样是在老外博客上找到的,分享给大家,再次感叹老外牛逼啊. 原文地址:http://gam ...

  7. 用Apache生产csr申请证书

    一. 安装Apache: 1.安装完成后将apache安装目录下 conf 文件夹中的"openssl.cnf"文件复制到bin文件夹中: 2.配置Apache支持ssl: 打开A ...

  8. XML:使用DOM技术解析xML文件中的城市,实现select级联选择

    中国的城市xml格式:cities.xml <?xml version="1.0" encoding="utf-8"?> <china> ...

  9. CXF WebService整合SpringMVC的maven项目

    首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html   http://blog.csdn.net/hu_shengyang/article/de ...

  10. 可爱的Python_课后习题_CDay0 时刻准备着!发布

    请根据软件发布的流程和软件开发的编码规范,将读者在前面章节所写的程序修改并发 布出去.另外,可以查找下除了 epydoc 外还有哪些较好的 py 文档生成器? pydoc是Python自带的模块,主要 ...