Как считать строки файла в C++?

Может быть, что-то вроде этого:

Sub CopyOverMatches()

    Dim tbl1 As ListObject, tbl2 As ListObject
    Dim h As ListColumn, h2 As ListColumn, data

    Set tbl1 = ActiveSheet.ListObjects("Table1")
    Set tbl2 = ActiveSheet.ListObjects("Table2")

    'loop over the headers from the source table
    For Each h In tbl1.ListColumns

        'is there a column of the same name in the destination table?
        Set h2 = Nothing
        On Error Resume Next
        Set h2 = tbl2.ListColumns(h.Name)
        On Error GoTo 0

        If Not h2 Is Nothing Then '<< have a matching column

            data = h.DataBodyRange.Value

            With h2.DataBodyRange.Cells(1).Resize(UBound(data, 1), 1)
                .NumberFormat = h.DataBodyRange(1).NumberFormat
                .Value = data
            End With

        End If
    Next h

End Sub
23
задан tshepang 4 November 2012 в 23:55
поделиться

7 ответов

Как насчет этого: -

  std::ifstream inFile("file"); 
  std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>(), '\n');
99
ответ дан 29 November 2019 в 00:38
поделиться

Вы читаете файл построчно. Подсчитайте количество прочитанных строк.

12
ответ дан 29 November 2019 в 00:38
поделиться

методы ядра, следующие за @Abhay

Полный код, который я сделал:

size_t count_line(istream &is)
{
    // skip when bad
    if( is.bad() ) return 0;  
    // save state
    std::istream::iostate state_backup = is.rdstate();
    // clear state
    is.clear();
    std::istream::streampos pos_backup = is.tellg();

    is.seekg(0);
    size_t line_cnt;
    size_t lf_cnt = std::count(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>(), '\n');
    line_cnt = lf_cnt;
    // if the file is not end with '\n' , then line_cnt should plus 1  
    is.unget();
    if( is.get() != '\n' ) { ++line_cnt ; } 

    // recover state
    is.clear() ; // previous reading may set eofbit
    is.seekg(pos_backup);
    is.setstate(state_backup);

    return line_cnt;
}

он не изменит состояние потока исходного файла, включая обработку ситуации \ n'-miss для последней строки.

0
ответ дан 29 November 2019 в 00:38
поделиться
int aNumOfLines = 0;
ifstream aInputFile(iFileName); 

string aLineStr;
while (getline(aInputFile, aLineStr))
{
    if (!aLineStr.empty())
        aNumOfLines++;
}

return aNumOfLines;
-2
ответ дан 29 November 2019 в 00:38
поделиться

int numLines = 0;
ifstream in("file.txt");
//while ( ! in.eof() )
while ( in.good() )
{
   std::string line;
   std::getline(in, line);
   ++numLines;
}

Возникает вопрос, как вы относитесь к самой последней строке файла, если она не заканчивается новой строкой. В зависимости от того, что вы делаете, вы можете посчитать это, а можете нет. Этот код считает это.

См .: http://www.cplusplus.com/reference/string/getline/

.
-3
ответ дан 29 November 2019 в 00:38
поделиться

Разделите размер файла на среднее количество символов в строке!

-10
ответ дан 29 November 2019 в 00:38
поделиться

Это правильная версия ответа Крейга У. Райта:

int numLines = 0;
ifstream in("file.txt");
std::string unused;
while ( std::getline(in, unused) )
   ++numLines;
10
ответ дан 29 November 2019 в 00:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: