vutils.cpp 861 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "vutils.h"
  2. #include <QFile>
  3. #include <QDebug>
  4. VUtils::VUtils()
  5. {
  6. }
  7. QString VUtils::readFileFromDisk(const QString &filePath)
  8. {
  9. QFile file(filePath);
  10. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  11. qWarning() << "error: fail to read file" << filePath;
  12. return QString();
  13. }
  14. QString fileText(file.readAll());
  15. file.close();
  16. qDebug() << "read file content:" << filePath;
  17. return fileText;
  18. }
  19. bool VUtils::writeFileToDisk(const QString &filePath, const QString &text)
  20. {
  21. QFile file(filePath);
  22. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  23. qWarning() << "error: fail to open file" << filePath << "to write to";
  24. return false;
  25. }
  26. QTextStream stream(&file);
  27. stream << text;
  28. file.close();
  29. qDebug() << "write file content:" << filePath;
  30. return true;
  31. }