![](/img/trans.png)
[英]How to go the end of the current line in a file and check the character in c++?
[英]How to check last line of file for new_line character? C++
如何检查文件的最后一行是否包含'\\ n'(或换行符)。
这是两个示例: 末尾带有换行符 的文件 - 末尾没有换行符的文件
我当前的代码:
fstream file("filename");
string line;
if (!file.is_open()) throw Exception();
while(getline(file, line))
{
(checking for lastline)
}
我意识到getline将不包含new_line字符。 我可以仔细检查每个字符,但是会出现性能问题。 有些文件包含数百万个字符,但我不知道如何转到最后一行以获取new_line字符。
- 编辑 -
您可以使用seekg跳到文件中的任何位置。
file.seekg(-1,ios_base::end); // go to one position before the EOF
char c;
file.get(c); // Read current character
if(c=='\n'){
cout<<"yes"<<endl; // You have new_line character
}
因此,我们跳到EOF之前的一个位置并读取了最后一个字符。 如果是新行,则完成。
有三种不同的方法来指示新行。
不要使用getline(),它将占用换行符。 在二进制模式下使用read()(请参阅Cheers和hth。-Alf注释)。 文本模式将替换每个新的行标记CR LF,以及CR到LF。 在您的示例中,您具有CR LF标签。
在二进制模式下,您必须去掉一个或两个字符减去文件长度,然后read()两个字符,然后检查它们等于CR LF。 请参阅Rishit示例。
getline的问题在于它读取行并将其放入std :: string中,但会删除换行符。 您将需要使用二进制模式读取功能。 最困难的任务是使其找到所有可能的新行组合,并使用各种文件大小,最后使其看起来优雅。 以下是我的尝试方法。
问题是,例如,如果您的平台将新行存储为'\\ r \\ n',那么如果\\ n或\\ r也算作最后一行的新行?
http://coliru.stacked-crooked.com/a/06f70dd4ef5c63c8
std::ofstream ofs("test.txt");
ofs << "test \n" << "test 2\n";
//ofs << "\r";
ofs.close();
std::ifstream ifs("test.txt", std::ifstream::binary);
// Read last two chars, it might also read only one last char
std::vector<char> end_file_chars;
for (int pos = 1; pos <= 2; ++pos) {
if (!ifs.seekg(-pos, std::ios::end)) break;
char c;
if (ifs.get(c)) end_file_chars.insert(end_file_chars.begin(), c);
}
// Possible end file characters
std::vector<std::vector<char>> endlines = {{'\r', '\n'},
{'\n'},
{'\r'}};
// Predicate to compare possible endline with what was found in the file.
auto checkFn = [&](auto &endline) {
// Equal compares possible endline in reverse order
return std::equal(endline.rbegin(), endline.rend(), end_file_chars.rbegin());
};
// If any end file character was read and if it acually is end file character...
if (!end_file_chars.empty() && std::find_if(endlines.begin(), endlines.end(),checkFn) != endlines.end()) {
std::cout << "Found";
}
else {
std::cout << "Not Found";
}
您可以使用
fgets(string_name, buffer_size, stdin)
与gets()和fgets()包含new_line字符不同,
与puts()不同,fputs()排除了new_line字符
http://www.cplusplus.com/reference/cstdio/fgets/
例:
while( fgets(str, sizeof(str), stdin) ) {
// check newline at end of string
int len = strlen(str);
if( str[ len-1 ] != '\0' ) {
str[ len-1 ] = '\0'; // make sure there's no new_line at the end
len--;
}
// now check for empty string, if thus, then last line
if( strcmp(str, "") == 0 ) break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.