我正在尝试检查文件是否成功打开。我发现这种方法可以打开要读取的文件:
char path[]="data/configuration.txt";
std::ifstream configFile(path, std::ifstream::in);
if(configFile) {
std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
问题是究竟要if检查什么?
因为我还发现了以下检查文件是否打开的方法:
char path[]="data/configuration.txt";
std::ifstream configFile;
configFile.open(path, std::ifstream::in);
if(configFile.is_open()) {
std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我还有其他问题。例如,两种打开文件的方法有什么区别?另外,这两个if条件有什么区别?
我认为这些是相似的方法,最终会得到相同的结果,因为我可以使用两种打开方法std::ifstream一样的is_open方法:
std::ifstream configFile(path, std::ifstream::in);
configFile.open(path, std::ifstream::in);
Run Code Online (Sandbox Code Playgroud) 下午好 !
我正在尝试制作某种圆形堆栈。它应该像一个普通的 LIFO 堆栈,但没有明显的限制。它应该消除或跳过当时引入的第一个元素,而不是达到最大容量!
例如:
假设我们有一个包含 3 个元素的堆栈:stack[3]
我们通过将 3 个元素“推入”内部来填充它:push[a], push[b], push[c]。
但随后我们需要添加第四个和第五个元素:push[d], push[e]。
标准堆栈会说堆栈已达到极限,无法添加更多元素。
但我想要一个循环堆栈,它将消除或跳过aand b,记住c,dande并输出e,dand c;
该项目是在 ESP32 上的 PlatformIO 中完成的,因此我无法访问 C++ STL,即使可以访问,我也认为仅为 1 个堆栈编译这么大的库是没有意义的。即使曾经有一段时间我认为我应该编译一个类似的库来让我访问stackor deque,但那个时候已经过去了,因为现在我感觉自己像一个无法解决数学问题的白痴。这已经困扰我一个多星期了。
我在网上找到的只是以下 FIFO 循环缓冲区:
class circular_buffer {
public:
explicit circular_buffer(size_t size) :
buf_(std::unique_ptr<T[]>(new T[size])),
max_size_(size)
{
}
void put(T item)
{
std::lock_guard<std::mutex> lock(mutex_);
buf_[head_] = item;
if(full_) {
tail_ …Run Code Online (Sandbox Code Playgroud)