C++_輸入和輸出示例
阿新 • • 發佈:2022-05-05
輸入/輸出庫
定義於標頭檔案 <ios> 定義於標頭檔案 <iostream> 定義於標頭檔案 <fstream> 定義於標頭檔案 <sstream> 定義於標頭檔案 <spanstream> 定義於標頭檔案 <syncstream> C++ 亦包含 C 所定義的輸入/輸出函式,如 std::fopen 、 std::getc 等。 C 流為 std::FILE 型別物件所代表,只能通過 std::FILE* 型別指標訪問及操作。 fopen fclose 定義於標頭檔案 <cstdio>
型別轉換到string
C ++11提供了 std::to_string #include <iostream> #include <string> // std::to_string int main() { std::string pi = "pi is" + std::to_string(3.1415926); std::cout << pi << std::endl; return 0; } 其他 sprintf()是個c的函式,當然c++相容c也可以用,c語言在stdio.h中,c++在cstdio中 std::sprintf(buf, "%d", value) std::sprintf(buf, "%f", value) sprintf的作用是將一個格式化的字串輸出到一個目的字串中,而printf是將一個格式化的字串輸出到螢幕
輸出方式
std::cout << " raw -> " << "type: " << type << ", " << "conf: " << conf << ", " << std::endl; //-----######## //標頭檔案:<stdlib.h> std::string outfile = "/opt/output.txt"; FILE *out_result = fopen(outfile.c_str(),"a"); fprintf(out_result,"%s%s%s%s%f \n"," test->","type:",type.c_str(),",conf:",conf ); fclose(fp);
寫入到檔案
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main( void )
{
//利用ofstream類的建構函式建立一個檔案輸出流物件來開啟檔案 -//write results to txt
std::string outfile = "/opt/output.txt";
std::ofstream fout(outfile );
if ( fout.is_open() )
{
fout << "Learning C++ is very useful."<< endl;
fout.close();
}
return 0;
}
//----------------示例-------
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
void main( void )
{
std::string outfile = "/opt/output.txt";
FILE *out_result = fopen(outfile.c_str(),"a");
fprintf(out_result,"%s%s%s%s%f \n"," test->","type:",type.c_str(),",conf:",conf );
fclose(fp);
return 0;
}
Linux資料夾
1.巨集定義 實際上這提供了一種抽象機制,使程式碼可以在不同平臺間移植。
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
2.使用標頭檔案 direct.h 中的 access 和 mkdir 函式
#include <direct.h>
#include <iostream>
using namespace std;
int main()
{
string folderPath = "E:\\database\\testFolder";
// if this folder not exist, create a new one.
if (0 != access(folderPath.c_str(), 0))
{
mkdir(folderPath.c_str()); // 返回 0 表示建立成功,-1 表示失敗
}
return 0;
}
3.資料夾
基本流程:opendir-->readdir-->closedir
#include <iostream>
#include <vector>
#include <algorithm>
#include <dirent.h>
#include <string>
#include <cstring>
using namespace std;
struct ImageFile
{
std::string file_name; /*!< 檔名 */
int order_number; /*!< 檔案序號, 取檔名 */
};
bool SortByImageFiles(const ImageFile img1, const ImageFile img2)
{
return img1.order_number < img2.order_number;
}
vector<string> split(const string& str, const string& delim)
{
vector<string> res;
if ("" == str) return res;
char* strs = new char[str.length() + 1];
strcpy(strs, str.c_str());
char* d = new char[delim.length() + 1];
strcpy(d, delim.c_str());
char* p = strtok(strs, d);
while (p) {
string s = p;
res.push_back(s);
p = strtok(NULL, d);
}
return res;
}
/**
* @brief 字元分割函式
* @param folder_path 資料夾路徑
* @param file_format 檔案型別
* @param file_names ImageFile型別的vector,用於存放圖片集合
*/
void GetImagePaths(char *folder_path, char *file_format, std::vector<ImageFile>& file_names) {
DIR *dp;
struct dirent *dirp;
if ((dp=opendir(folder_path))==NULL)
printf("cant open folder");
ImageFile temp_path;
vector<string> split_result;
std::string file_name;
while (((dirp = readdir(dp))) != NULL)
{
file_name = dirp->d_name;
split_result = split(file_name, ".");
if (split_result.size() >= 1)
{
if(split_result[1] == file_format){
temp_path.file_name = folder_path;
temp_path.file_name += file_name;
temp_path.order_number = atoi(split_result[0].c_str());
file_names.push_back(temp_path);
}
}
}
std::sort(file_names.begin(), file_names.end(), SortByImageFiles);
}
int main() {
std::vector<ImageFile> file_names;
GetImagePaths("/home/test_img/", "jpg", file_names);
for (auto lin : file_names) {
cout << lin.file_name << endl;
}
return 0;
}
參考
C++獲取指定路徑資料夾下的所有圖片-windows和linux下各自的處理方案 https://blog.csdn.net/a2824256/article/details/112302856
C 風格檔案輸入/輸出 https://zh.cppreference.com/w/cpp/io/c