二进制文件读写

二进制读文件

image.png

二进制写文件

image.png

在文件中写入并读取一个整数

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. void main() {
  5. ofstream fout("some.dat", ios::out | ios::binary);
  6. int x = 120;
  7. fout.write( (const char *) (&x), sizeof(int) );
  8. fout.close();
  9. ifstream fin("some.data", ios::in | ios::binary);
  10. int y;
  11. fin.read((char*) & y, sizeof(int));
  12. fin.close();
  13. cout << y << endl;
  14. }

从键盘输入几个学生信息,并以二进制文件保存

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct Student {
  5. char name[20];
  6. int score;
  7. };
  8. void main() {
  9. Student s;
  10. ofstream OutFile("c:\\tmp\\students.dat", ios::out | ios::binary);
  11. while(cin >> s.name >> s.score)
  12. OutFile.write( (char*) & s, sizeof(s));
  13. OutFile.close();
  14. }

image.png

将students.dat文件的内容读出并显示

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct Student {
  5. char name[20];
  6. int score;
  7. };
  8. void main() {
  9. Student s;
  10. ifstream inFile("students.dat", ios::in | ios::binary);
  11. if(!inFile) {
  12. cout << "error" << endl;
  13. return 0;
  14. }
  15. while(inFile.read( (char*) & s, sizeof(s))) {
  16. int readedBytes = inFile.gcount(); // 看刚才读了多少字节
  17. cout << s.name << " " << s.score << endl;
  18. }
  19. ifFile.close();
  20. }

将students.dat文件的Jane的名字改成Mike

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct Student {
  5. char name[20];
  6. int score;
  7. };
  8. void main() {
  9. Student s;
  10. fstream iofile("c:\\tmp\\students.dat" ios::in | ios::out | ios::binary);
  11. if(!iofile) {
  12. cout << "error\n";
  13. return
  14. }
  15. iofile.seekp( 2 * sizeof(s), ios::beg); // 定位写指针到第三个记录
  16. iofile.write("Mike", strlen("Mike")+1); // +1是多写一个'\0'
  17. iofile.seekg(0, ios::beg); // 定位读指针到开头
  18. while(iofile.read( (char*) & s, sizeof(s)))
  19. cout << s.name " " << s.score << endl;
  20. iofile.close();
  21. }
  22. /* 输出
  23. Tom 60
  24. Jack 80
  25. Mike 40
  26. */

二进制文件的优点

  1. 节省存储空间:字符文件每一个字符就是1个字节,而二进制文件不然
    1. 一个10int类型数据,二进制文件需要4个字节,而字符文件需要10*1=10个字节
  2. 查找方便:二进制文件查找需要算得的字节偏移量好算,根据数据类型的sizeof()大小即可算出,而字符型数据不容易算

    文件拷贝程序mycopy实例

    1. /* 用法示例:
    2. mycopy src.dat dest.dat
    3. 将src.dat拷贝到dest.dat 如果dest.dat原来就有,则原来的文件会被覆盖
    4. */
    5. #include <iostream>
    6. #include <fstream>
    7. using namespace std;
    8. int main(int argc, char * argv[]) {
    9. if(argc != 3) {
    10. cout << "File name missing!" << endl;
    11. return 0;
    12. }
    13. ifstream inFile(argv[1], ios::binary | ios::in);
    14. if(!inFile) {
    15. cout << "Source file open error." << endl;
    16. return 0;
    17. }
    18. ostream outFile(argv[2], ios::binary | ios::out);
    19. if(!outFile) {
    20. cout << "New file open error." << endl;
    21. inFile.close(); // 打开文件必须关闭
    22. return 0;
    23. }
    24. char c;
    25. while(inFile.get(c)) // 每次读取一个字符,操作系统会把硬盘邻近区域全部取到内存缓冲区
    26. outFile.put(c); // 每次写入一个字符
    27. outFile.close();
    28. inFile.close();
    29. return 0;
    30. }

    二进制文件和文本文件的区别

    image.png
    image.png