我有一个函数来读取一行中的一个变量(整数,双或布尔)的值ifstream:
template <typename Type>
void readFromFile (ifstream &in, Type &val)
{
string str;
getline (in, str);
stringstream ss(str);
ss >> val;
}
Run Code Online (Sandbox Code Playgroud)
但是,对于在第一行开头插入BOM(字节顺序标记)的编辑器创建的文本文件失败,遗憾的是它包含{Note,Word} pad.如何修改此函数以忽略字节顺序标记(如果存在于开头)str?
当我使用函数readTheNRow with row = 0(我读第一行)时,我发现三个第一个字符是\ 357,\ 273和\ 277.我发现这个前缀是与UTF-8文件有关的一些,但有些文件有这个前缀,有些文件没有:(.我如何忽略我想从中读取的文件中所有类型的此类前缀?
int readTheNRow(char buff[], int row) {
int file = open("my_file.txt", O_RDONLY);
if (file < 0) {
write(2, "closing fifo was unsuccessful\n", 31);
exit(-1);
}
// function's variables
int i = 0;
char ch; // a temp variable to read with it
int check; // helping variable for checking the read function
// read till we reach the needed row
while (i != row) {
// read one char
check = read(file, &ch, …Run Code Online (Sandbox Code Playgroud)