用fstream读取和写入c中的二进制文件
发布时间:2020-12-16 10:09:34 所属栏目:百科 来源:网络整理
导读:我正在尝试编写简单的c代码来读写文件. 问题是我的输出文件小于原始文件,我找不到原因. 我有一个6.6 kb的图像,我的输出图像大约6.4 kb #include iostream#include fstreamusing namespace std;ofstream myOutpue;ifstream mySource;int main(){ mySource.ope
我正在尝试编写简单的c代码来读写文件.
问题是我的输出文件小于原始文件,我找不到原因. 我有一个6.6 kb的图像,我的输出图像大约6.4 kb #include <iostream> #include <fstream> using namespace std; ofstream myOutpue; ifstream mySource; int main() { mySource.open("im1.jpg",ios_base::binary); myOutpue.open("im2.jpg",ios_base::out); char buffer; if (mySource.is_open()) { while (!mySource.eof()) { mySource >> buffer; myOutpue << buffer; } } mySource.close(); myOutpue.close(); return 1; } 解决方法
您的代码中有3个问题:
1-您尚未在二进制文件中打开输出文件. 2-你的代码返回“1”,通常你应该返回“0”,如果出现问题然后返回错误代码. 3-你应该使用“操纵器”并使c不要避免空格,所以为了从文件中读取而不是: mySource >> buffer; 你应该使用: mySource >> std:noskipws >> buffer; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |