GCC和MSVC之间的文件输出不一致
File output inconsistency between GCC and MSVC
考虑以下类,这是一个简单的文件加载类,用于通过将数据加载到构造过程中来操纵小文本文件。
#include <fstream>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
class file_loader {
public:
/**
* brief Constructs a file_loader instance using a given filename. Caches the file contents
* into the internal cached storage container.
*
* param _filename Name/directory of file to load.
* param _max_line_length Optional, approximate maximum length of file lines.
*/
file_loader(const std::string& _filename, std::size_t _max_line_length = static_cast<std::size_t>(256)) : fs(_filename), filename(_filename) {
cache_contents(_max_line_length);
}
/**
* brief Deleted copy construction, copy constructing is forbidden.
*/
file_loader(const file_loader& _other) = delete;
/**
* brief Move constructor, moves a given file_loader instance to this leaving the
* parameterised instance in a valid but unspecified state.
*
* param _other rvalue reference to a file_loader instance to move.
*/
file_loader(file_loader&& _other) : fs(std::move(_other.fs)), filename(std::move(_other.filename)),
cached_contents_vec(std::move(_other.cached_contents_vec)) { }
/**
* brief Writes all changes made to the internal cached storage to the
* filestream, overwriting the current contents of the file.
*/
void write_changes() {
fs.close();
fs.open(filename);
fs.clear();
write_cache();
}
/**
* brief Reads a given line of the internal cached storage. This internal
* store is guaranteed to always be up to date, no call to write_changes
* is required to maintain consistency.
*
* param _n Line number to read.
* return const reference to std::string instance given by _n'th line.
* throws Throws std::out_of_range exception if _n exceeds lines in internal cached storage.
*/
const std::string& read_line(std::size_t _n) const {
if (_n >= cached_contents_vec.size())
throw std::out_of_range("File: " + filename + " does not have " + std::to_string(_n) + " lines.");
return cached_contents_vec[_n];
}
/**
* brief Overwrites a given line of the internal cached storage such that
* the next call to write_changes will update the file contents.
*
* param _n Line number to overwrite.
* param _str std::string instance to overwrite current line contents with.
* throws Throws std::out_of_range exception if _n exceeds lines in internal cached storage.
*/
void overwrite_line(std::size_t _n, const std::string& _str) {
if (_n >= cached_contents_vec.size())
throw std::out_of_range("File: " + filename + " does not have " + std::to_string(_n) + " lines.");
cached_contents_vec[_n] = _str;
}
/**
* brief Erases a given line of the internal cached storage such that
* the next call to write_changes will update the file contents.
*
* param _n Line number to erase.
* return Iterator to next valid position in internal cached storage container.
*/
auto erase_line(std::size_t _n) {
return cached_contents_vec.erase(cached_contents_vec.begin()+_n);
}
/**
* brief Deleted copy assignment operator, copy assignment is forbidden.
*/
file_loader& operator=(const file_loader& _other) = delete;
/**
* brief Move assignment operator, uses move-semantics to move the parameterised
* file_loader instance to this. Instance being moved is left in a
* valid but unspecified state.
*
* param _other rvalue reference to file_loader instance.
*/
file_loader& operator=(file_loader&& _other) {
// check for self-assignment
if (this != &_other) {
fs = std::move(_other.fs);
filename = std::move(_other.filename);
cached_contents_vec = std::move(_other.cached_contents_vec);
}
return *this;
}
private:
std::fstream fs;
std::string filename;
std::vector<std::string> cached_contents_vec; // internal cached storage container
/**
* brief Caches contents of files into the internal cached storage container.
*
* param _max_line_length Approximate maximum line length of the file, used for performance improvements.
*/
void cache_contents(std::size_t _max_line_length) {
std::string line_str;
// reserve space for performance
line_str.reserve(_max_line_length);
while (std::getline(fs, line_str)) {
cached_contents_vec.push_back(line_str);
}
}
/**
* brief Convenience method for writing a std::vector<std::string> to an std::ostream instance.
*
* param _os Instance of std::ostream.
* param _vec Instance of std::vector<std::string> to write to _os.
* return Reference to _os modified with data of _vec.
*/
void write_cache() {
for (const auto& x : cached_contents_vec) {
fs << x << "n";
}
}
};
使用此类使用GCC (6.1.0)
和MSVC (2015)
执行时似乎会产生不同的结果。说我有以下文本文件(test.txt
):
cheese
ham
eggs
beef
bacon
和以下main
:
int main(void) {
file_loader fl("test.txt");
fl.overwrite_line(3, "chicken");
fl.write_changes();
}
在MSVC
中,它产生了我预期的结果:
cheese
ham
eggs
chicken
bacon
但是,在GCC (6.1.0)
中,我得到:
cheese
ham
eggs
chickenbacon
换句话说,后者似乎"串联"了覆盖线,下面的线和下面的线成为cached_contents_vec
的单个条目,而前者则将条目保持分开(因为在这种情况下应该据我所知)。
有什么想法在这里发生了什么?我还应该提到,在file_loader::write_changes()
中,MSVC不需要此方法中的两个初始行来覆盖先前的文件内容,而GCC则不需要。
正如@igortandetnik所说的,这里的问题是在GCC生成Unix式的线端端,而MSVC产生了基于Windows的线端端。当在简单的文本编辑器中查看输出时,例如Notepad,GCC的UNIX式输出不会被视为线路指示灯的末端,而在"更好"的编辑器(例如Notepad )中打开时,它会正确处理相同的输出。两种情况。
相关文章:
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- C++对象数组的文件输出
- bcdedit 文件输出不起作用,但其他命令可以
- 如何从文本文件输出数据?(学生成绩单计划)
- 使用 C++文件 I/O 输出 pdf 文件
- C 文件输出垃圾
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++:具有随机名称的文件输出
- 在命令提示符下看到换行符,但在文件输出中可以看到 相同的字符串
- 生成文件不会生成预期的文件输出
- C++11 fgetc 在"r+b"模式下使用时向我的文件输出 0
- 使用函数从文件输出
- GCC.obj文件输出不具有确定性(.debug_info,PROGPITS部分)
- C cstdiofile :: Writestring给出了不正确的文件输出
- C 输出:STD :: COUT和文件输出没有相同的内容
- GCC和MSVC之间的文件输出不一致
- C++文件输出奇怪的数字,并且部分代码没有运行
- 如何将特定字符从TXT文件输出到特定点
- C 调用从TXT文件输出信息的函数
- 将交易从文本文件输出到屏幕