Code C++ Xử lý file

Chào mừng các bạn đến với blog: Phát Triển Phần Mềm 123AZ

Đây là các mẫu code cho việc xử lý file trong lập trình c++

+ Kiểm tra sự tồn tại 1 file
#include <conio.h>
#include <io.h>
#include <iostream>

bool ChecIsExistFile(const char* path) 
{
  bool check = _access(path, 0);
  if (check == -1)
    return false;
  else
    return true;
}

void main() 
{
  bool ret = ChecIsExistFile("text1.txt"); 
  _getch();
}

 

+ Delete 1 file trong c++
#include <iostream>
#include <conio.h>

bool DeleteFile(const char* file_path)
{
  int ret = remove(file_path);
  bool is_ok = (ret  == 0) ? true : false;
  return ret;
}


void main()
{
  bool ret = DeleteFile("abc.txt");
  _getch();
	return;
}
+ Tạo 1 file với một dung lượng mong muốn Byte – KB – MB

=> Dùng cho nhiều trường hợp cần để test

#include <iostream>
#include <conio.h>
#include <fstream>

using namespace std;
char sample_text64[] = {"128character128character128character128character128character128c"};
void CreateFile(const char* path, const char& ch, const int& num) 
{
  fstream wr_file;
  wr_file.open(path, ios::out);

  switch (ch)
  {
  case 'B':
    {
      for(int i = 0; i < num; i++)
      {
        wr_file << "a" ;
        if(i > 0 && (i%500 == 0)) 
        {
          wr_file << "\n" ;
        }
      }
    }
    break;
  case 'K':
    {
      for (int i = 0;i <(num*16);i++) 
      {
        wr_file << sample_text64;
        if((i > 0) &&( i%512 == 0)) 
        {
          wr_file << "\n";
        }
      }
    }
    break;
  case 'M':
    {
      for (int i = 0; i<(num*1024*16); i++) 
      {
        wr_file << sample_text64;
        if ((i>0) && (i%512 == 0)) 
        {
          wr_file <<"\n";
        }
      }
    }
    break;
  default:
    break;
  }

  wr_file.close();
}


void main() 
{
  CreateFile("Test.txt", 'B', 100);
  CreateFile("Test2.txt", 'K', 100);
  CreateFile("Test3.txt", 'M', 100);
}
+ Liệt kê các file trong 1 folder có sẵn.
#include <Windows.h>
#include <conio.h>
#include <io.h>
#include <string>
#include <vector>
#include <iostream>

std::string WcharToString(wchar_t* wchar_str) 
{
  std::string str("");
  int index = 0;
  while (wchar_str[index] != 0) 
  {
    str += (char)wchar_str[index];
    ++ index;
  }
  return str;
}

wchar_t* StringToWchar(std::string str) 
{
  int index = 0;
  int count = str.size();
  wchar_t *ws_str = new wchar_t [count+1]; 
  while (index < str.size()) 
  {
    ws_str[index] = (wchar_t)str[index];
    index++;
  }
  ws_str[index] = 0;
  return ws_str;
}

std::vector<std::string> ListFileInFolder(std::string path_folder) 
{
  WIN32_FIND_DATA find_file_data;

  std::vector<std::string> list_file;
  wchar_t *path_folder_full = StringToWchar(path_folder);

  HANDLE hFind = FindFirstFile(path_folder_full, &find_file_data);
  list_file.push_back(WcharToString(find_file_data.cFileName));
  while(FindNextFile(hFind, &find_file_data)) 
  {
    list_file.push_back(WcharToString(find_file_data.cFileName));
  }
  return list_file;
}

void main() {
  std::string str_path_folder("C:\\Temp_Test\\*txt");

  std::vector<std::string> list_file = ListFileInFolder(str_path_folder);

  for (int i = 0; i<list_file.size(); i++) 
  {
    std::cout << list_file[i] << std::endl; 
  }
  _getch();
}
+ Copy nội dung file A vào File B
#include <conio.h>
#include <io.h>
#include <iostream>
#include <fstream>

bool CopyContentFileToFile(const char* src_path, const char* des_path) 
{

  std::ifstream::pos_type size;
  char * memblock = NULL;

  std::ifstream file_src (src_path, std::ios::in|std::ios::binary|std::ios::ate);
  if (file_src == NULL) 
  {
    return false;
  }

  if (file_src.is_open()) 
  {
    size = file_src.tellg();
    memblock = new char [size];
    file_src.seekg (0, std::ios::beg);
    file_src.read (memblock, size);
    file_src.close();
  }

  if (memblock != NULL)
  {
    std::fstream file_des(des_path, std::ios::out | std::ios::binary);
    file_des.seekg(0, std::ios::beg);  
    file_des.write(memblock, size);
    file_des.close();
  }
  
  return true;
}

void main() 
{
  bool ret = CopyContentFileToFile("Test2.txt", "C:\\Temp_Test\\t1.txt"); 
  _getch();
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.