Skip to content

Commit 5026d61

Browse files
committed
fix(count_file_lines): handle files that don't end with a newline
When counting newline characters with the std::istreambuf_iterator, files that don't ended with a newline were undercounted by one. This change should fix this issue.
1 parent 042cf27 commit 5026d61

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

comms/torchcomms/TorchCommUtils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ int count_file_lines(const std::string& filepath, bool ignore_empty_lines) {
9191
std::istreambuf_iterator<char>(filestream),
9292
std::istreambuf_iterator<char>(),
9393
'\n');
94+
95+
// Clear the stream state so we can use tellg/seekg
96+
filestream.clear();
97+
98+
// Check if the file is empty
99+
const bool is_empty_file =
100+
filestream.tellg() == std::ifstream::pos_type(0) &&
101+
filestream.peek() == std::ifstream::traits_type::eof();
102+
103+
// If the file does not end with a newline, we need to add one to the count
104+
if (!is_empty_file) {
105+
filestream.seekg(-1, std::ios::end);
106+
char last_char;
107+
filestream.get(last_char);
108+
if (last_char != '\n') {
109+
++line_count;
110+
}
111+
}
94112
}
95113

96114
if (filestream.bad()) {

0 commit comments

Comments
 (0)