C++打开文件似乎在运行后破坏了文件

标签 c++ fstream

简而言之,我双击其文件中的 image1 并打开它。我运行下面的代码打开 image1 但什么也没有出现。所以我再次进入带有 image1 的文件,双击它,Windows 照片查看器说,“Windows 照片查看器无法显示这张图片,因为文件是空的。”我用另外两个测试图像做了这个,同样的事情正在发生。没有丢失任何重要内容,但此方法似乎正在删除它试图打开的任何文件,我很好奇为什么以及如何修复它。

#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>

void main()
{
    std::ofstream imagetest;
    imagetest.open("C:\\Users\\Filepath\\image1.jpg");
    std::chrono::milliseconds dura(2000);
    std::this_thread::sleep_for(dura);//Kept the sleep in because I didn't know if having the console up would affect the file/image from opening. 
}

最佳答案

C++ 的级别低于脚本。打开并不意味着开始。 您必须使用 START C:\Users\Filepath\image1.jpg 执行批处理脚本。 或者学习更多的库来用 C++ 做这件事...

关于C++打开文件似乎在运行后破坏了文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26388393/

相关文章:

c++ - 在 solaris 中运行可执行的 c++ 程序

c++ - Fstream 不保存文件中的最后一个单词,也不从文件中读取

c++ - std::ofstream 的更快替代品

c++ - while 循环因 eof 检查停止得太晚

C++ fstream 不读取整个文件

c++ - 为什么 ofstream 不触发 IN_CREATE

c++ - 使用 QuickFIX 指定 FIX 消息中的字段顺序

c++ - 使用 operator[] 访问 unique_ptr 的私有(private) std::map

c++ - 递归 typedef 函数定义:std::function 返回自己的类型

C++ 错误 : ‘string’ does not name a type