C++_输入和输出示例

输入/输出库

 定义于头文件 <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++ 17 定义于命名空间 std::filesystem  文件系统库原作为 boost.filesystem并最终从 C++17 开始并入 ISO C++ 。
         现在 boost 实现可用的编译器和平台于多于 C++17 库
		 Boost库是一个优秀的、可移植、开源的第三方C++库

 历史的情况
  01.Using C api in windows, include <sys/stat.h> and <sys/types.h>   include <direct.h>
   
  02.Using boost library 
   ASSERT(boost::filesystem::status(_root).type() == boost::filesystem::directory_file);
   Boost可为大致归入以下分类:
     输入输出 字符串和文本处理库 数学和数字
     容器库  算法库 数据结构   迭代器库
     函数对象和高阶编程库  #include <boost/lambda/lambda.hpp>
     泛型编程  模板元编程  预处理元编程  并发编程  排错和测试
     图像处理 跨语言混合编程  内存管理  解析  编程接口
     杂项 编译器问题的变通方案

     输入输出库
	   #include <iostream>
	 文件系统库
	  
     字符串和文本处理库 
	      std::string 
	 容器库
	    #include <vector>
     正则表达库 #include <regex>
	 
	 第三方库
	      libjpeg-turbo    https://libjpeg-turbo.org/   x86 和 x86-64 处理器优化的高速 libjpeg 的改进版本
		  opencv       读取图像底层实现就是利用 libJPEG 库, 

参考

  C++获取指定路径文件夹下的所有图片-windows和linux下各自的处理方案 https://blog.csdn.net/a2824256/article/details/112302856
  C 风格文件输入/输出 https://zh.cppreference.com/w/cpp/io/c
posted @ 2022-05-05 18:05  辰令  阅读(283)  评论(0编辑  收藏  举报