Может быть, что-то вроде этого:
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
Как насчет этого: -
std::ifstream inFile("file");
std::count(std::istreambuf_iterator<char>(inFile),
std::istreambuf_iterator<char>(), '\n');
Вы читаете файл построчно. Подсчитайте количество прочитанных строк.
методы ядра, следующие за @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 для последней строки.
int aNumOfLines = 0;
ifstream aInputFile(iFileName);
string aLineStr;
while (getline(aInputFile, aLineStr))
{
if (!aLineStr.empty())
aNumOfLines++;
}
return aNumOfLines;
int numLines = 0;
ifstream in("file.txt");
//while ( ! in.eof() )
while ( in.good() )
{
std::string line;
std::getline(in, line);
++numLines;
}
Возникает вопрос, как вы относитесь к самой последней строке файла, если она не заканчивается новой строкой. В зависимости от того, что вы делаете, вы можете посчитать это, а можете нет. Этот код считает это.
.Разделите размер файла на среднее количество символов в строке!
Это правильная версия ответа Крейга У. Райта:
int numLines = 0;
ifstream in("file.txt");
std::string unused;
while ( std::getline(in, unused) )
++numLines;