Skip to content

Commit 3caf608

Browse files
committed
fix(util): 添加ReadFistLineFromTextFile(),并修复读取文件行存在多余的\r的问题
1 parent d5c9895 commit 3caf608

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

modules/util/fs.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,33 @@ bool ReadEachLineFromTextFile(const std::string &filename, const std::function<v
7373
try {
7474
ifstream f(filename);
7575
if (f) {
76-
std::string line;
77-
while (std::getline(f, line))
78-
line_handle_func(line);
76+
std::string text;
77+
while (std::getline(f, text)) {
78+
while (!text.empty() && (text.back() == '\r' || text.back() == '\n'))
79+
text.pop_back();
80+
line_handle_func(text);
81+
}
82+
return true;
83+
} else {
84+
LogWarn("open failed, %s", filename.c_str());
85+
}
86+
} catch (const exception &e) {
87+
LogWarn("catch exception: %s", e.what());
88+
}
89+
return false;
90+
}
91+
92+
bool ReadAllLinesFromTextFile(const std::string &filename, std::vector<std::string> &text_vec)
93+
{
94+
try {
95+
ifstream f(filename);
96+
if (f) {
97+
std::string text;
98+
while (std::getline(f, text)) {
99+
while (!text.empty() && (text.back() == '\r' || text.back() == '\n'))
100+
text.pop_back();
101+
text_vec.emplace_back(std::move(text));
102+
}
79103
return true;
80104
} else {
81105
LogWarn("open failed, %s", filename.c_str());
@@ -86,14 +110,14 @@ bool ReadEachLineFromTextFile(const std::string &filename, const std::function<v
86110
return false;
87111
}
88112

89-
bool ReadAllLinesFromTextFile(const std::string &filename, std::vector<std::string> &lines)
113+
bool ReadFirstLineFromTextFile(const std::string &filename, std::string &text)
90114
{
91115
try {
92116
ifstream f(filename);
93117
if (f) {
94-
std::string line;
95-
while (std::getline(f, line))
96-
lines.emplace_back(std::move(line));
118+
std::getline(f, text);
119+
while (!text.empty() && (text.back() == '\r' || text.back() == '\n'))
120+
text.pop_back();
97121
return true;
98122
} else {
99123
LogWarn("open failed, %s", filename.c_str());

modules/util/fs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ bool ReadEachLineFromTextFile(const std::string &filename, const std::function<v
7474
*/
7575
bool ReadAllLinesFromTextFile(const std::string &filename, std::vector<std::string> &lines);
7676

77+
/**
78+
* 从文件中读取第一行文本
79+
*
80+
* \param filename 文件名
81+
* \param text 读出的行内容
82+
*
83+
* \return true 文件打开成功
84+
* \return false 文件打开失败
85+
*/
86+
bool ReadFirstLineFromTextFile(const std::string &filename, std::string &text);
87+
7788
/**
7889
* 将字串写入到文件
7990
*

modules/util/fs_test.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ TEST(fs, TextFileReadAppend_Ok) {
7171

7272
TEST(fs, TextFileReadEachLine) {
7373
::unlink(test_filename); //! 先删一次
74-
const std::string text_tobe_write = "first line\nsecond line\nthird line";
74+
const std::string text_tobe_write = "first line\r\nsecond line\nthird line";
7575
EXPECT_TRUE(WriteStringToTextFile(test_filename, text_tobe_write)); //! 写入数据
7676
std::vector<std::string> str_vec;
7777
bool ret = ReadEachLineFromTextFile(test_filename,
@@ -84,6 +84,19 @@ TEST(fs, TextFileReadEachLine) {
8484
EXPECT_EQ(str_vec[2], "third line");
8585
}
8686

87+
TEST(fs, TextFileReadFirstLine) {
88+
::unlink(test_filename); //! 先删一次
89+
EXPECT_TRUE(WriteStringToTextFile(test_filename, "first line\r\n")); //! 写入数据
90+
std::string text;
91+
EXPECT_TRUE(ReadFirstLineFromTextFile(test_filename, text));
92+
ASSERT_EQ(text, "first line");
93+
94+
::unlink(test_filename); //! 先删一次
95+
EXPECT_TRUE(WriteStringToTextFile(test_filename, "one line")); //! 写入数据
96+
EXPECT_TRUE(ReadFirstLineFromTextFile(test_filename, text));
97+
ASSERT_EQ(text, "one line");
98+
}
99+
87100
TEST(fs, FileFail) {
88101
EXPECT_FALSE(WriteStringToTextFile(test_filename_cannot_access, "Hello"));
89102
std::string text_tobe_read;

0 commit comments

Comments
 (0)