C++ (Qt)#include <QCoreApplication>#include <QFile>#include <QTextStream>#include <iostream> //! Error codesenum ErrorType { //! None errors errNone = 0, //! Error of the opening of a file errFileOpen = -1000, //! Error of the writing of a file errFileWrite = -1001, //! Wrong data errDataBad = -5000}; ErrorType readData( const QString &fileName, QString &data );ErrorType writeResult( const QString &fileName, QString &data );void showData( const QString &data );ErrorType showError( const QString &fileName, ErrorType errorCode ); /*! * Working with a text file */int main( int argc, char *argv[] ) { QCoreApplication a( argc, argv ); QString data; ErrorType errorCode = ErrorType::errNone; // Read data from a file QString iFileName = QString( "input.txt" ); errorCode = readData( iFileName, data ); if ( errorCode != ErrorType::errNone ) { return showError( iFileName, errorCode ); } // Show data on the screen showData( data ); // Data processing data += QString( ", World" ); // Write data to a file QString oFileName = QString( "output.txt" ); writeResult( oFileName, data ); if ( errorCode != ErrorType::errNone ) { return showError( oFileName, errorCode ); } return a.exec( );} /*! * Read data from the file * \param fileName File name * \param data data * \return Error code */ErrorType readData( const QString &fileName, QString &data ) { QFile file( fileName ); if ( !file.open( QIODevice::ReadOnly ) ) { return ErrorType::errFileOpen; } data = file.readAll( ); file.close( ); return ErrorType::errNone;} /*! * Write data to the file * \param fileName Fleile name * \param data Data * \return Error code */ErrorType writeResult( const QString &fileName, QString &data ) { QFile file( fileName ); if ( !file.open( QIODevice::WriteOnly ) ) { return ErrorType::errFileOpen; } QTextStream stream( &file ); stream << data; file.close( ); if ( stream.status( ) != QTextStream::Ok ) { return ErrorType::errFileWrite; } return ErrorType::errNone;} /*! * Show data on the screen * \param data Data */void showData( const QString &data ) { std::cout << data.toStdString( ) << std::endl;} /*! * Write the error code to the file * \param fileName File name * \param errorCode Error code * \return Error code */ErrorType showError( const QString &fileName, ErrorType errorCode ) { switch ( errorCode ) { case ErrorType::errFileOpen: std::cerr << "Error: cannot open the file " << fileName.toStdString( ) << std::endl; break; case ErrorType::errFileWrite: std::cerr << "Error: cannot write to the file " << fileName.toStdString( ) << std::endl; break; case ErrorType::errDataBad: std::cerr << "Error: incorrect data in the file " << fileName.toStdString( ) << std::endl; break; default: std::cerr << "Error code: " << errorCode << "; file name: " << fileName.toStdString( ) << std::endl; break; } return errorCode;}
C++ (Qt)#include <QCoreApplication>#include <QFile>#include <QTextStream>#include <QVector>#include <iostream>#include <exception> void readData( const QString &fileName, QVector<int> &arr )throw (std::invalid_argument, std::runtime_error); void writeResult( const QString &fileName, QVector<int> &arr )throw (std::invalid_argument, std::runtime_error); void showData( const QVector<int> &arr ); /*! * Working with a text file */int main( int argc, char *argv[] ) { QCoreApplication a( argc, argv ); // Read data from a file QString iFileName = QString( "input.txt" ); QVector<int> arr; try { readData( iFileName, arr ); } catch ( const std::exception &e ) { std::cerr << e.what( ) << std::endl; return 1; } // Show data on the screen showData( arr ); // Data processing arr.push_back( 100 ); // Write data to a file QString oFileName = QString( "output.txt" ); try { writeResult( oFileName, arr ); } catch ( const std::exception &e ) { std::cerr << e.what( ) << std::endl; return 1; } return a.exec( );} /*! * Read data from the file * \param fileName File name * \param data data */void readData( const QString &fileName, QVector<int> &dest )throw (std::invalid_argument, std::runtime_error) { QFile file( fileName ); if ( !file.open( QIODevice::ReadOnly ) ) { std::string error = "Unable to open the file " + fileName.toStdString( ); throw std::invalid_argument( error ); } QTextStream stream( &file ); QString input; do { stream >> input; bool ok; int value = input.toInt( &ok ); if ( (ok) && (stream.status( ) == QTextStream::Ok) ) { dest.push_back( value ); } else if ( !stream.atEnd( ) ) { std::string error = "Unable to read file " + fileName.toStdString( ); throw std::runtime_error( error ); } } while ( !stream.atEnd( ) );} /*! * Write data to the file * \param fileName Fleile name * \param data Data * \return Error code */void writeResult( const QString &fileName, QVector<int> &arr )throw (std::invalid_argument, std::runtime_error) { QFile file( fileName ); if ( !file.open( QIODevice::WriteOnly ) ) { std::string error = "Unable to open the file " + fileName.toStdString( ); throw std::invalid_argument( error ); } QTextStream stream( &file ); for ( int i = 0; i < arr.size( ); ++i ) { stream << arr[i]; if ( i != (arr.size( ) - 1) ) { stream << " "; } else { stream << endl; } } file.close( ); if ( stream.status( ) != QTextStream::Ok ) { std::string error = "Unable to write to the file " + fileName.toStdString( ); throw std::runtime_error( error ); }} /*! * Show data on the screen * \param data Data */void showData( const QVector<int> &arr ) { for ( int i = 0; i < arr.size( ); ++i ) { std::cout << QString::number( arr[i] ).toStdString( ) << std::endl; }}
C++ (Qt)#-------------------------------------------------## Project created by QtCreator 2014-03-16T19:25:22##------------------------------------------------- QT += core QT -= gui QMAKE_CXXFLAGS += -std=c++11 TARGET = text_fileCONFIG += consoleCONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp HEADERS += \ FileError.h \ FileOpenError.h \ FileReadError.h \ FileWriteError.h
C++ (Qt)#ifndef FILEERROR_H#define FILEERROR_H #include <QString>#include <exception>#include <string> class FileError : public std::runtime_error {public: FileError( const QString &fileIn ) : std::runtime_error( "" ), mFile( fileIn ) { } virtual const char* what( ) const noexcept { return mMsg.c_str( ); } QString getFileName( ) const { return mFile; } protected: QString mFile; std::string mMsg;}; #endif // FILEERROR_H
C++ (Qt)#ifndef FILEOPENERROR_H#define FILEOPENERROR_H #include <QString>#include "FileError.h" class FileOpenError : public FileError {public: FileOpenError( const QString &fileNameIn ) : FileError( fileNameIn ) { //mMsg = QString("Unable to open ") + fileNameIn; mMsg = "Unable to open " + fileNameIn.toStdString( ); }}; #endif // FILEOPENERROR_H
C++ (Qt)#ifndef FILEREADERROR_H#define FILEREADERROR_H #include "FileError.h"#include <QString>#include <QTextStream>#include <sstream> class FileReadError : public FileError {public: FileReadError( const QString &fileNameIn, int lineNumIn ) : FileError( fileNameIn ), mLineNum( lineNumIn ) { // QTextStream stream(stdout); // //stream << QString("Error reading ") << fileNameIn << QString(" at line ") << lineNumIn; // mMsg = QString("Error reading %1 at line %2").arg(fileNameIn).arg(lineNumIn); // stream >> mMsg; std::ostringstream ostr; ostr << "Error reading " << fileNameIn.toStdString( ) << " at line " << lineNumIn; mMsg = ostr.str( ); } int getLineNum( ) const { return mLineNum; } protected: int mLineNum;}; #endif // FILEREADERROR_H
C++ (Qt)#ifndef FILEWRITEERROR_H#define FILEWRITEERROR_H #include <QString>#include "FileError.h" class FileWriteError : public FileError {public: FileWriteError( const QString &fileNameIn ) : FileError( fileNameIn ) { //mMsg = QString( "Unable to write " ) + fileNameIn; mMsg = "Unable to write " + fileNameIn.toStdString( ); }}; #endif // FILEWRITEERROR_H
C++ (Qt)#include <QCoreApplication>#include <QFile>#include <QTextStream>#include <QVector>#include <iostream>#include "FileOpenError.h"#include "FileReadError.h"#include "FileWriteError.h" void readData( const QString &fileName, QVector<int> &arr )throw (FileOpenError, FileReadError); void writeResult( const QString &fileName, QVector<int> &arr )throw (FileOpenError, FileWriteError); void showData( const QVector<int> &arr ); /*! * Working with a text file */int main( int argc, char *argv[] ) { QCoreApplication a( argc, argv ); // Read data from a file QString iFileName = QString( "input.txt" ); QVector<int> arr; try { readData( iFileName, arr ); } catch ( const FileError &e ) { std::cerr << e.what( ) << std::endl; return 1; } // Show data on the screen showData( arr ); // Data processing arr.push_back( 100 ); // Write data to a file QString oFileName = QString( "output.txt" ); try { writeResult( oFileName, arr ); } catch ( const FileError &e ) { std::cerr << e.what( ) << std::endl; return 1; } return a.exec( );} /*! * Read data from the file * \param fileName File name * \param data data */void readData( const QString &fileName, QVector<int> &dest )throw (FileOpenError, FileReadError) { QFile file( fileName ); if ( !file.open( QIODevice::ReadOnly ) ) { throw FileOpenError( fileName ); } QTextStream stream( &file ); QString input; int lineNumber = 0; do { lineNumber++; stream >> input; bool ok; int value = input.toInt( &ok ); if ( (ok) && (stream.status( ) == QTextStream::Ok) ) { dest.push_back( value ); } else if ( !stream.atEnd( ) ) { throw FileReadError( fileName, lineNumber ); } } while ( !stream.atEnd( ) );} /*! * Write data to the file * \param fileName Fleile name * \param data Data * \return Error code */void writeResult( const QString &fileName, QVector<int> &arr )throw (FileOpenError, FileWriteError) { QFile file( fileName ); if ( !file.open( QIODevice::WriteOnly ) ) { throw FileOpenError( fileName ); } QTextStream stream( &file ); for ( int i = 0; i < arr.size( ); ++i ) { stream << arr[i]; if ( i != (arr.size( ) - 1) ) { stream << " "; } else { stream << endl; } } file.close( ); if ( stream.status( ) != QTextStream::Ok ) { throw FileWriteError( fileName ); }} /*! * Show data on the screen * \param data Data */void showData( const QVector<int> &arr ) { for ( int i = 0; i < arr.size( ); ++i ) { std::cout << QString::number( arr[i] ).toStdString( ) << std::endl; }}
C++ (Qt) do { stream >> input; bool ok; int value = input.toInt( &ok ); if ( (ok) && (stream.status( ) == QTextStream::Ok) ) { dest.push_back( value ); } else if ( !stream.atEnd( ) ) { std::string error = "Unable to read file " + fileName.toStdString( ); throw std::runtime_error( error ); } } while ( !stream.atEnd( ) );}
C++ (Qt)stream >> dest; // вместо всей бадяги выше
C++ (Qt)using namespace xxx; int main( int argc, char *argv[] ){ try { Application app; app.parseCommandLine( argc, argv ); app.isRunning(); app.daemonize(); app.checkCfgFile(); app.exec(); } catch( const std::exception &x ) { Log( "%s", x.what() ); return 1; } catch( ... ) // Специальная ветка для Вереса, что бы ничего не падало тихо. :) { Log( "Shit! Unknow exception. :)" ); return -1; } return 0;}
C++ (Qt)try{ Stream stream( ... ); stream >> id; stream >> data1; stream >> data2;}catch( const ErrorTypeOne &ex ){}catch( const ErrorTypeTwo &ex ){}