file_operation.cpp
Go to the documentation of this file.
1 #include "file_operation.h"
2 #include <dirent.h>
3 #include <unistd.h>
4 #include <fstream>
5 #include <iostream>
6 #include "common_config.h"
7 #include <algorithm>
8 #include <cstring>
9 #include <sstream>
12 
13 // Name the file with test time,Time_t is accurate to the second.
14 std::string FileOperation::setFileName(std::string file_type)
15 {
16  const time_t t = time(NULL);
17  struct tm* systemtime = localtime(&t);
18  std::string file_name = "/" + std::to_string(test_file_id_) + "-" + std::to_string(1900 + systemtime->tm_year) + "-" +
19  std::to_string(1 + systemtime->tm_mon) + "-" + std::to_string(systemtime->tm_mday) + "-" +
20  std::to_string(systemtime->tm_hour) + "-" + std::to_string(systemtime->tm_min) + "-" +
21  std::to_string(systemtime->tm_sec) + file_type;
22  test_file_id_++;
23  return file_name;
24 };
25 
26 struct DataBuff FileOperation::readFileAsDatabuffer(const std::string file_path)
27 {
28  struct DataBuff data_buff;
29  std::ifstream file(file_path, std::ios::binary);
30 
31  if (file.is_open())
32  {
33  file.seekg(0, file.end);
34  data_buff.size = file.tellg();
35 
36  file.seekg(0, file.beg);
37  data_buff.data = new char[data_buff.size];
38  file.read(data_buff.data, data_buff.size);
39  }
40  else
41  {
42  std::cout << "Fail to open expected audio file: " << file_path << std::endl;
44  }
45  file.close();
46  return data_buff;
47 }
48 
49 std::string FileOperation::readFileAsString(const std::string file_path)
50 {
51  std::fstream file(file_path, std::ios::in | std::ios::binary);
52  std::stringstream sstream;
53 
54  sstream << file.rdbuf();
55  file.close();
56 
57  std::string str(sstream.str());
58  sstream.clear();
59 
60  return str;
61 }
62 
63 // 获取某目录下所有文件名
64 void FileOperation::getAllFilesName(std::string dir_path, std::vector<std::string>& files_name)
65 {
66  DIR* dir;
67  struct dirent* ptr;
68  char base[1000];
69 
70  if ((dir = opendir(dir_path.c_str())) == NULL)
71  {
72  std::cout << ("Open dir error...");
74  }
75 
76  while ((ptr = readdir(dir)) != NULL)
77  {
78  if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
79  {
80  continue; //跳过'.'和'..'两个目录
81  }
82  else if (ptr->d_type == 8)
83  {
84  files_name.push_back(ptr->d_name);
85  }
86  else if (ptr->d_type == 10)
87  {
88  continue; // 跳过链接文件
89  }
90  else if (ptr->d_type == 4)
91  {
92  files_name.push_back(ptr->d_name);
93  }
94  }
95  closedir(dir);
96  //排序,按从小到大排序
97  sort(files_name.begin(), files_name.end());
98 }
99 
100 void CSVOperation::readNextRow(std::istream& str)
101 {
102  std::string line;
103  std::getline(str, line);
104  std::stringstream lineStream(line);
105  std::string cell;
106 
107  m_data.clear();
108  while (std::getline(lineStream, cell, ','))
109  {
110  m_data.push_back(cell);
111  }
112  // This checks for a trailing comma with no data after it.
113  if (!lineStream && cell.empty())
114  {
115  // If there was a trailing comma then add an empty element.
116  m_data.push_back("");
117  }
118 }
119 
120 std::vector<std::vector<std::string>> CSVOperation::readAllCSV(std::istream& infile)
121 {
122  std::string row;
123  while (!infile.eof())
124  {
125  if (infile.bad() || infile.fail())
126  {
127  std::cout << "Fail to open the CSV file! " << std::endl;
128  exit(ERROR_FILE_OPEN_FAIL);
129  }
130  readNextRow(infile);
131  csv_table_.push_back(m_data);
132  }
133  return csv_table_;
134 }
135 
136 void CSVOperation::writeRowData(std::string csv_file, std::vector<std::string> row_vector)
137 {
138  int size = row_vector.size();
139  std::ofstream outFile(csv_file, std::ios::app);
140  if (outFile.is_open())
141  {
142  for (int i = 0; i < size; i++)
143  {
144  outFile << row_vector[i] << ',';
145  }
146  outFile << std::endl;
147  }
148  else
149  {
150  std::cout << "Fail to open the CSV file! " << std::endl;
151  exit(ERROR_FILE_OPEN_FAIL);
152  }
153  outFile.close();
154 }
std::string readFileAsString(const std::string file_path)
std::string setFileName(std::string file_type)
char * data
Definition: file_operation.h:8
std::vector< std::vector< std::string > > readAllCSV(std::istream &str)
void readNextRow(std::istream &str)
void writeRowData(std::string csv_file, std::vector< std::string > row_vector)
void getAllFilesName(std::string dir_path, std::vector< std::string > &files_name)
struct DataBuff readFileAsDatabuffer(const std::string file_path)


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53