我有两个文本文件,它们在文本编辑器中看起来是相同的,但我用于读取文件的 C++ 代码为每个文件生成不同的行数。我无法弄清楚这些文件在哪里不同,或者如何在我的 C++ 代码中适应这种差异。
让我解释一下...
我有两个文本文件,d1.txt 和 d2.txt。每个包含 100 个数字,每行 1 个。当我在 vim 中打开任一文件并输入 :set list!
时,只有 100 行,每行包含一个数字以及每行最后一个数字后面的行结束符 ($)。换句话说,当在 vim 中查看它们时,它们看起来相同,只是数字精度不同。精度不同,因为一个文件来自 MATLAB,另一个文件来自 Gnumeric。
文件的快速比较呈现以下输出(为了节省空间,我使用大括号省略号“[...]”省略部分):
1,28c1,28
< 0.01218465532007
[...]
< 0.01327976337895
---
> 0.0121846553200678
[...]
> 0.0132797633789485
30,100c30,100
< 0.01329705254301
[...]
< 0.00017832496354
---
> 0.0132970525430057
[...]
> 0.000178324963543758
\ No newline at end of file
尽管有关于第二个文件 (d2.txt) 末尾缺少换行符的消息,但在 vim 中检查文件的最后几行时我看不到任何差异,正如我上面提到的。
我创建了一个 C++ 函数 readVectorFromFile(std::vector<double>&,const string)
返回从相应文本文件读取的行数。当我使用代码读取文本文件时:
std::cout << "d1.txt has " << readVectorFromFile(v1,"./d1.txt") << " lines.\n";
std::cout << "d2.txt has " << readVectorFromFile(v1,"./d1.txt") << " lines.\n";
我得到输出:
d1.txt has 99 lines.
d2.txt has 100 lines.
该函数的定义方式如下:
int readVectorFromFile(vector<double>& vec, const string& fullFilePathName) {
int value, numLines;
char line[10000];
ifstream inFile;
/* attempt to open file */
inFile.open(fullFilePathName.c_str());
if (inFile.fail()) {
LOG(FATAL) << "Unable to open file \"" << fullFilePathName.c_str() << "\" for reading.";
} else {
cout << "Importing vector from file " << fullFilePathName.c_str() << "\n";
}
/* records the number of lines in the input file */
numLines = static_cast<int>( count(istreambuf_iterator<char>(inFile),
istreambuf_iterator<char>(), '\n') );
/* start file over from beginning */
inFile.clear();
inFile.seekg(0, ios::beg);
vec.clear(); // clear current vec contents
vec.reserve(numLines);
/* read value from each line of file into vector */
for(int i=0; i<numLines; ++i) {
inFile.getline(line, 10000);
vec.push_back( strtod(line,NULL) );
}
inFile.close(); // close filestream
return numLines; // return the number of lines (values) read
}
为什么我在 vim 中查看这些文件时看不到它们之间的区别?上述函数有什么根本性的错误导致了这个问题吗?
最佳答案
根据您的描述,两个文件之一的末尾没有换行符。您可以使用 od -c file | less
查看文件。查看文件的确切内容,包括其字符代码。
也就是说,您读取行的方法可能可以改进:只需读取一行,检查它是否可以读取,然后处理它。这样,就不需要计算前面的行结尾数:
for (std::string line; std::getline(inFile, line); ) {
vec.push_back(strtod(line.c_str()));
}
就我个人而言,我可能会首先阅读这些数字,例如:
for (double value; inFile >> value; ) {
vec.push_back(value);
}
嗯,这并不是读取 double
序列的真正方法。 s 到 vector 中,但这是:
std::vector<double> vec((std::istream_iterator<double>(inFile)),
std::istream_iterator<double>());
(您可以在 C++11 中使用统一初始化表示法,而不是额外的括号)。
关于c++ - 由于隐藏/控制字符,在 C++ 中读取文本文件行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18435133/