C++ (Qt)#include <iostream>#include <fstream>#include <iterator>#include <algorithm>#include <stdexcept> /***************************************************************************/ int string_count(const std::string& fname) { std::ifstream file(fname); if ( !file ) { throw std::runtime_error("can`t open file: " + fname); } return std::distance( (std::istream_iterator<std::string>(file)), (std::istream_iterator<std::string>()) );} int main(int argc, const char** argv) { std::cout << "strings = " << string_count("main.cpp") << std::endl; std::cin.get(); return 0;} /***************************************************************************/
C++ (Qt)QTextStream stream(&file);// количество строк.quint64 linecount=0x00;QString line; do { line = stream.readLine(); // увеличим счетчик строк if(!line.isNull()) { linecount++;} } while (!line.isNull());
C++ (Qt) return std::distance( (std::istream_iterator<std::string>(file)), (std::istream_iterator<std::string>()) );
C++ (Qt)QFile f("in.txt");int n = 0;if (f.open(QIODevice::ReadOnly | QIODevice::Text) n = QTextStream(&f).readAll().split('\n').count();
QFile f("in.txt");int n = -1;if (f.open(QIODevice::ReadOnly | QIODevice::Text) { QByteArray buf; do { buf = f.readLine(); n++; } while (!buf.isEmpty());}
int n = 0;QFile file("in.txt");if (file.open(QFile::ReadOnly)) { char buf[1024]; // либо QVarLengthArray - для произвольной длины строки qint64 lineLength; forever { lineLength = file.readLine(buf, sizeof(buf)); if (lineLength > 0) ++n; else if (lineLength == -1) //### else break; }}
#include <iostream>#include <fstream>#include <iterator>#include <algorithm>#include <stdexcept>#include <QtGlobal>#include <QTime>#include <QFile>#include <QTextStream>#include <QStringList>using namespace std;/**** sendevent's variant.** i think it's possible to get a correct "endl" from the Qt, but idk how =(*/#if defined(Q_OS_MAC) #define CURRENT_LINES_DELEMITER "\r"#elif defined(Q_OS_WIN) #define CURRENT_LINES_DELEMITER "\r\n"#elif defined(Q_OS_LINUX) #define CURRENT_LINES_DELEMITER "\n"#else message("define your lines ending!")#endif //-- Q_OS_MAC|Q_OS_WINunsigned long sendevent( const char* pFileName ){ QFile file( pFileName ); unsigned long res = 0; if( file.open( QIODevice::ReadOnly ) ) { res = file.readAll().count( CURRENT_LINES_DELEMITER ); file.close(); } return res;}/**** nixMan's variant.** google it yourselth how to change it for compille with MS VS** and ignore whitespaces ;-)*/unsigned long nixMan( const char* pFileName ) { ifstream file( pFileName ); if ( !file ) { throw runtime_error( string( "can`t open file: " ).append( pFileName ) ); } return (unsigned long)distance( (istream_iterator< string >(file)), (istream_iterator< string >()) );}/**** garryHotDog's lines counter*/unsigned long garryHotDog( const char *pFileName ){ QFile file( pFileName ); unsigned long linecount=0x00; if( file.open( QIODevice::ReadOnly ) ) { QTextStream stream( &file ); // количество строк. QString line; do { line = stream.readLine(); // увеличим счетчик строк if(!line.isNull()) { linecount++;} } while (!line.isNull()); file.close(); } return linecount;}unsigned long kambala( const char *pFileName ){ unsigned long n = 0; QFile f( pFileName ); if ( f.open(QIODevice::ReadOnly | QIODevice::Text ) ) { n = QString( f.readAll() ).split(CURRENT_LINES_DELEMITER).count(); f.close(); } return n;}unsigned long konstantin( const char *pFileName ){ unsigned long n = 0; QFile file( pFileName ); if ( file.open( QFile::ReadOnly ) ) { char buf[1024]; // либо QVarLengthArray - для произвольной длины строки qint64 lineLength; forever { lineLength = file.readLine(buf, sizeof(buf)); if (lineLength > 0) { ++n; } else if (lineLength == -1) { break; } } file.close(); } return n;}/**** test stuff*/typedef unsigned long ( *pointer2function )( const char* );const char* callTestFunction( pointer2function pFunc, const char *pArg ){ QTime time; unsigned long iLinesCount, iMsecs; time.start(); iLinesCount = pFunc( pArg ); iMsecs = time.elapsed(); QString strRes( "%1 lines were found at %2 ms" ); return strRes.arg( iLinesCount ).arg( iMsecs ).toAscii();}void createTestFile( const char *pFileName, const char *line, unsigned long ulLinesAmount ){ QFile file( pFileName ); if( file.open( QIODevice::WriteOnly ) ) { QTextStream txtStream( &file ); for( unsigned long i = 0; i < ulLinesAmount; ++i ) { txtStream << line << i+1 << CURRENT_LINES_DELEMITER; } file.close(); cout << endl << "The " << pFileName << " contains " << ulLinesAmount << " lines \""<< line << "\"" << endl; }}void test( const char* pFile, const char *pLine, unsigned long ulLinesCount, int iTests ){ string strFileName( pFile ); strFileName.append( ".txt" ); const char *pFileName = strFileName.c_str(); createTestFile( pFileName, pLine, ulLinesCount ); pointer2function ptr2Func = NULL; for( int i = 0; i < iTests; ++i ) { ptr2Func = &::nixMan; cout << callTestFunction( ptr2Func, pFileName ) << " by nixMan" << endl; ptr2Func = &::garryHotDog; cout << callTestFunction( ptr2Func, pFileName ) << " by harryHotDog" << endl; ptr2Func = &::sendevent; cout << callTestFunction( ptr2Func, pFileName ) << " by sendevent" << endl; ptr2Func = &::kambala; cout << callTestFunction( ptr2Func, pFileName ) << " by kambala" << endl; ptr2Func = &::konstantin; cout << callTestFunction( ptr2Func, pFileName ) << " by konstantin" << endl; }}int main(int /*argc*/, const char** /*argv*/) { unsigned long ulShortTest = 1024; unsigned long ulLongTest = ulShortTest*ulShortTest; test( "testShortWithoutSpaces", "line_line", ulShortTest, 1 ); test( "testShortWithSpaces", "line line", ulShortTest, 1 ); test( "testLongtWithoutSpaces", "line_line", ulLongTest, 1 ); test( "testLongtWithSpaces", "line line", ulLongTest, 1 ); return 0;}