Reading and writing binary file

前端 未结 7 592
挽巷
挽巷 2020-11-22 16:29

I\'m trying to write code to read a binary file into a buffer, then write the buffer to another file. I have the following code, but the buffer only stores a couple of ASCI

相关标签:
7条回答
  • 2020-11-22 16:38

    sizeof(buffer) is the size of a pointer on your last line NOT the actual size of the buffer. You need to use "length" that you already established instead

    0 讨论(0)
  • 2020-11-22 16:42

    It can be done with simple commands in the following snippet.

    Copies the whole file of any size. No size constraint!

    Just use this. Tested And Working!!

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
      ifstream infile;
      infile.open("source.pdf",ios::binary|ios::in);
    
      ofstream outfile;
      outfile.open("temppdf.pdf",ios::binary|ios::out);
    
      int buffer[2];
      while(infile.read((char *)&buffer,sizeof(buffer)))
      {
          outfile.write((char *)&buffer,sizeof(buffer));
      }
    
      infile.close();
      outfile.close();
      return 0;
    }
    

    Having a smaller buffer size would be helpful in copying tiny files. Even "char buffer[2]" would do the job.

    0 讨论(0)
  • 2020-11-22 16:44

    There is a much simpler way. This does not care if it is binary or text file.

    Use noskipws.

    char buf[SZ];
    ifstream f("file");
    int i;
    for(i=0; f >> noskipws >> buffer[i]; i++);
    ofstream f2("writeto");
    for(int j=0; j < i; j++) f2 << noskipws << buffer[j];
    

    Or you can just use string instead of the buffer.

    string s; char c;
    ifstream f("image.jpg");
    while(f >> noskipws >> c) s += c;
    ofstream f2("copy.jpg");
    f2 << s;
    

    normally stream skips white space characters like space or new line, tab and all other control characters. But noskipws makes all the characters transferred. So this will not only copy a text file but also a binary file. And stream uses buffer internally, I assume the speed won't be slow.

    0 讨论(0)
  • 2020-11-22 16:47

    If you want to do this the C++ way, do it like this:

    #include <fstream>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        std::ifstream input( "C:\\Final.gif", std::ios::binary );
        std::ofstream output( "C:\\myfile.gif", std::ios::binary );
    
        std::copy( 
            std::istreambuf_iterator<char>(input), 
            std::istreambuf_iterator<char>( ),
            std::ostreambuf_iterator<char>(output));
    }
    

    If you need that data in a buffer to modify it or something, do this:

    #include <fstream>
    #include <iterator>
    #include <vector>
    
    int main()
    {
        std::ifstream input( "C:\\Final.gif", std::ios::binary );
    
        // copies all data into buffer
        std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
    
    }
    
    0 讨论(0)
  • 2020-11-22 16:57

    You should pass length into fwrite instead of sizeof(buffer).

    0 讨论(0)
  • 2020-11-22 17:01
     sizeof(buffer) == sizeof(char*) 
    

    Use length instead.

    Also, better to use fopen with "wb"....

    0 讨论(0)
提交回复
热议问题