Output with QFile : QFile « Qt « C++






Output with QFile

  

#include <QtCore>
#include <iostream>
#include <stdio.h>
using namespace std;

int main(int argc, char* argv[])
{
  QFile in("c:\\a.txt");
  if(!in.open(QIODevice::ReadOnly|QIODevice::Text)) {
    cerr << "File " << argv[1] << " does not exist" << endl;
  }

  QFile out;
  out.setFileName(argv[2]);
  if (out.exists()) {
      cerr << "File" << argv[2] << " already exists" << endl;
      return 1;
  }
  if(!out.open(QIODevice::WriteOnly|QIODevice::Text)) {
      cerr << "Failed to open file " << argv[2] << " for writing" << endl;
      return 1;
  }
  while (!in.atEnd()) {
   QByteArray line = in.readLine();
   if (!line.trimmed().isEmpty() && !line.trimmed().startsWith('#'))
     out.write(line);
  }

  in.close();
  out.close();

  return 0;
}

   
    
  








Related examples in the same category

1.Create QFile from file path
2.Create file with QFile
3.Open QFile with WriteOnly flag
4.Check file existance and file name with QFile
5.Open a text file in read-only mode
6.Open binary file for write-operation
7.Read file till file end
8.Read text file line by line
9.Read byte array from text file
10.Open text file for write-operation