21xrx.com
2024-09-19 09:03:10 Thursday
登录
文章检索 我的文章 写文章
C++文件存储技巧分享
2023-07-05 11:05:29 深夜i     --     --
C++ 文件存储 技巧分享

C++是一种高级编程语言,可以创建各种类型的应用程序,包括图形用户界面、网络应用程序和游戏等。在C++编程中,文件存储技巧是非常重要的一个方面。在本文中,我将与读者分享一些C++文件存储技巧。

1.使用ifstream和ofstream读写文件

C++提供了两个用于文件操作的类:ifstream和ofstream。其中,ifstream用于读取文件内容,ofstream用于将数据写入文件。下面是使用ifstream和ofstream读写文件的示例代码:


#include <iostream>

#include <fstream>

int main()

{

  std::ofstream myfile("example.txt");

  myfile << "Hello, World!";

  myfile.close();

  std::ifstream infile("example.txt");

  std::string line;

  while (std::getline(infile, line))

  

    std::cout << line << std::endl;

  

  infile.close();

  return 0;

}

2.文件加密和解密

在C++中,可以使用fgets()函数读取文件内容,并使用fwrite()函数将加密后的数据写入文件。下面是使用fgets()函数和fwrite()函数进行文件加密和解密的示例代码:


#include <iostream>

#include <fstream>

#include <string.h>

char password[] = "mypassword";

void encrypt(char *input_file, char *output_file)

{

  FILE *fp_in = fopen(input_file, "rb");

  FILE *fp_out = fopen(output_file, "wb");

  if (fp_in == NULL || fp_out == NULL)

  

    std::cout << "Error opening file" << std::endl;

    return;

  

  char buffer[1024];

  int key_index = 0;

  while (fgets(buffer, 1024, fp_in))

  {

    for (int i = 0; i < strlen(buffer); i++)

    {

      buffer[i] ^= password[key_index];

      key_index++;

      if (key_index == strlen(password))

      

        key_index = 0;

      

    }

    fwrite(buffer, strlen(buffer), 1, fp_out);

  }

  fclose(fp_in);

  fclose(fp_out);

}

void decrypt(char *input_file, char *output_file)

{

  FILE *fp_in = fopen(input_file, "rb");

  FILE *fp_out = fopen(output_file, "wb");

  if (fp_in == NULL || fp_out == NULL)

  

    std::cout << "Error opening file" << std::endl;

    return;

  

  char buffer[1024];

  int key_index = 0;

  while (fgets(buffer, 1024, fp_in))

  {

    for (int i = 0; i < strlen(buffer); i++)

    {

      buffer[i] ^= password[key_index];

      key_index++;

      if (key_index == strlen(password))

      

        key_index = 0;

      

    }

    fwrite(buffer, strlen(buffer), 1, fp_out);

  }

  fclose(fp_in);

  fclose(fp_out);

}

int main()

{

  encrypt("example.txt", "encrypted.txt");

  decrypt("encrypted.txt", "decrypted.txt");

  return 0;

}

3.随机文件访问

在C++中,可以使用seekg()和seekp()函数实现随机文件访问。其中,seekg()函数用于将读取指针移动到指定位置,seekp()函数用于将写入指针移动到指定位置。下面是使用seekg()和seekp()函数进行随机文件访问的示例代码:


#include <iostream>

#include <fstream>

int main()

{

  std::ofstream myfile("example.dat", std::ios::out | std::ios::binary);

  if (!myfile)

  

    std::cout << "Error opening file" << std::endl;

    return 0;

  

  for (int i = 0; i < 10; i++)

  {

    myfile.write(reinterpret_cast<char*>(&i), sizeof(i));

  }

  myfile.close();

  std::ifstream infile("example.dat", std::ios::in | std::ios::binary);

  if (!infile)

  

    std::cout << "Error opening file" << std::endl;

    return 0;

  

  int value;

  infile.seekg(sizeof(int) * 3, std::ios::beg);

  infile.read(reinterpret_cast<char*>(&value), sizeof(value));

  std::cout << "Value at position 3: " << value << std::endl;

  infile.seekg(sizeof(int) * 6, std::ios::beg);

  infile.read(reinterpret_cast<char*>(&value), sizeof(value));

  std::cout << "Value at position 6: " << value << std::endl;

  infile.close();

  return 0;

}

以上是一些C++文件存储技巧的详细介绍,希望对大家有所帮助。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复