


How to quickly generate the text path of data? C++ implements text path generation
文本路径读取
在机器学习模型训练前期,需要对数据、图像、文本等进行预处理,而如何快速生成数据的文本路径呢?本文接下来直接使用C++实现文本路径生成,可查找固定格式如.jpg
、.txt
等文件路径(绝对路径或文件名),然后保存为.txt
文本,方便后期图片数据读取使用。
C++代码实现如下:
#include <io.h>#include <fstreanm>#include <string>#include <vector>#include <iostream>using namespace std;void GetAllFiles(string path, vector<string>& files, string format) { long hfile = 0; struct _finddata_t fileinfo; //用来存储文件信息的结构体 string p; if(hfile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) //第一次查找 { do{ //files.push_back(p.assign(fileinfo.name)); //只保存文件名files.push_back(p.assign(path).appand("\\").append(fileinfo.name)); //保存文件路径和文件名 }while(_findnext(hfile, &fileinfo) == 0); _findclose(hfile) } else if((hfile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do{ if((fileinfo.attrib & _A_SUBDIR)) //如果查找到的是文件夹 { if(strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) //进入文件夹查找 { GetAllFiles(p.assign(path).append("\\").append(fileinfo.name), files, format); } } else //如果查找的不是文件夹 { //files.push_back(p.assign(fileinfo.name)); //只保存文件名 files.push_back(p.assign(path).appand("\\").append(fileinfo.name)); //保存文件路径和文件名 } }while(_findnext(hfile, &fileinfo) == 0); _findclose(hfile) } }int main() { string filepath = "D:\\path..."; //文件根目录 vector<string> files; char *dstAll = "path.txt"; //读取所以格式为jpg的文件 string format = ".jpg"; GetAllFiles(filepath, files, format); ofstream ofn(distAll); int size = files.size(); for(int i = 0; i<size; i++) { ofn<<files[i]<<endl; //写入文件 cout<<files[i]<<endl; //输出到屏幕 } ofn.close(); cout<<"file number: "<<size<<endl; system("pause";) return 0; }
注意:如果format赋值出错会进入死循环。
相关文章:
The above is the detailed content of How to quickly generate the text path of data? C++ implements text path generation. For more information, please follow other related articles on the PHP Chinese website!

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
