Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读写c++;使用fstream 我试图编写一个简单的C++代码来读写文件。 问题是我的输出文件比原始文件小,我一直在寻找原因。 我有一个6.6KB的图像,我的输出图像大约是6.4KB #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; } #包括 #包括 使用名称空间std; myOutpue流; ifstreammysource; int main() { open(“im1.jpg”,ios_base::binary); myOutpue.open(“im2.jpg”,ios_base::out); 字符缓冲区; if(mySource.is_open()) { 而(!mySource.eof()) { mySource>>缓冲区; myOutpue_C++_Fstream_Ifstream_Ofstream - Fatal编程技术网 >缓冲区; myOutpue,c++,fstream,ifstream,ofstream,C++,Fstream,Ifstream,Ofstream" /> >缓冲区; myOutpue,c++,fstream,ifstream,ofstream,C++,Fstream,Ifstream,Ofstream" />

读写c++;使用fstream 我试图编写一个简单的C++代码来读写文件。 问题是我的输出文件比原始文件小,我一直在寻找原因。 我有一个6.6KB的图像,我的输出图像大约是6.4KB #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; } #包括 #包括 使用名称空间std; myOutpue流; ifstreammysource; int main() { open(“im1.jpg”,ios_base::binary); myOutpue.open(“im2.jpg”,ios_base::out); 字符缓冲区; if(mySource.is_open()) { 而(!mySource.eof()) { mySource>>缓冲区; myOutpue

读写c++;使用fstream 我试图编写一个简单的C++代码来读写文件。 问题是我的输出文件比原始文件小,我一直在寻找原因。 我有一个6.6KB的图像,我的输出图像大约是6.4KB #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; } #包括 #包括 使用名称空间std; myOutpue流; ifstreammysource; int main() { open(“im1.jpg”,ios_base::binary); myOutpue.open(“im2.jpg”,ios_base::out); 字符缓冲区; if(mySource.is_open()) { 而(!mySource.eof()) { mySource>>缓冲区; myOutpue,c++,fstream,ifstream,ofstream,C++,Fstream,Ifstream,Ofstream,两件事:忘记以二进制模式打开输出,并且不能使用输入/输出运算符>和为什么不: #include <fstream> int main() { std::ifstream mySource("im1.jpg", std::ios::binary); std::ofstream myOutpue("im2.jpg", std::ios::binary); myOutpue << mySource.rdbuf(); } #包括 int main()

两件事:忘记以二进制模式打开输出,并且不能使用输入/输出运算符
>
为什么不:

#include <fstream>

int main()
{
    std::ifstream mySource("im1.jpg", std::ios::binary);
    std::ofstream myOutpue("im2.jpg", std::ios::binary);
    myOutpue << mySource.rdbuf();
}
#包括
int main()
{
std::ifstreammysource(“im1.jpg”,std::ios::binary);
流myOutpue的std::of(“im2.jpg”,std::ios::binary);

myOutpue您的代码中有3个问题:

1-您尚未以二进制打开输出文件

2-代码返回“1”,通常应返回“0”,如果出现问题,则返回错误代码

3——你应该使用“操纵器”,并且使C++不避免空白,所以从文件中读取而不是:

mySource >> buffer;
你应使用:

mySource >> std:noskipws >> buffer;

嗯,这只是因为在图像的末尾添加了填充。任何文件的eof都不包括在文件末尾添加的填充字节。 试试这个 以img1.jpg结尾包含20个空格字符,此处不可见(UEGFUYREGREGERFCG6YGERBUCYKGEUGCRGFRGEYF)并运行您的程序(文件中不包括括号,这些用于显示数据内容) 您将看到img2.jpg包含(uegfuyregwfyugwrycgerfcg6ygerbucykgegugcrgfrgeyf)


因此,更好的选择是使用文件大小逐字节读取文件,您可以使用stat获得文件大小,然后运行for loop直到文件大小。希望这可以解决您上面提到的问题

我试图使用它来获取二进制字符,如:coutI试图使用它来获取二进制字符,如:cout@user3604079:这就是为什么你应该问你的问题,而不是完全无关的问题。请发布一个新的、不同的问题,并确保对它的回答确实对你有帮助。
mySource >> std:noskipws >> buffer;