【1】Read the file name under the sub-file and store it in TXT
(1) First understand a file structure:
struct _finddata_t {
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[260];
};
The type of time_t is long.
The type of _fsize_t is unsigned long.
Now let’s explain the data members of the structure.
attrib is the attribute of the file being searched for:
_A_ARCH(存档)
_A_HIDDEN(隐藏)
_A_NORMAL(正常)
_A_RDONLY(只读)
_A_SUBDIR(子文件夹)
_A_SYSTEM(系统)
time_create, time_access and time_write are respectively the time when the file was created, the time when the file was last accessed and the time when the file was last modified.
size: file size.
name: file name.
(2). Use _findfirst and _findnext to find files (both functions are in io.h.)
1、_findfirst函数:long _findfirst(const char *, struct _finddata_t *);
The first parameter is the file name. You can use "*.*" to find all files, or you can use "*.cpp" to find .cpp files. The second parameter is the _finddata_t structure pointer. If the search is successful, the file handle is returned; if it fails, -1 is returned.
2、_findnext函数:int _findnext(long, struct _finddata_t *);
The first parameter is the file handle, and the second parameter is also the _finddata_t structure pointer. If the search is successful, 0 is returned, otherwise -1 is returned.
3. _findclose() function: int _findclose(long);
There is only one parameter, the file handle. If the shutdown is successful, it returns 0, and if it fails, it returns -1.
Note: Under win10 system, findnext will report an error when compiling, as shown in the figure:
Because the return type of _findnext() is intptr_t instead of long, data is lost when converting from "intptr_t" to "long". Just generally write:
long handle
改为
intptr_t handle
既可以运行
code show as below:
#include<iostream>
#include<fstream>
#include <io.h>
using namespace std;
const char *to_search = "E:\\opencv学习\\学习opencv源码\\official-code-in-the-learning-Opencv3-master\\calibration\\*.jpg";//欲查找的文件,支持通配符(可选择文件类型)
//可以指定查找所有文件\\*.*,也可以是指定文件\\*.格式类型
int main()
{
//这里的handle句柄不要定义为long类型,否则_findnext会报错
intptr_t handle; //用于查找的句柄
struct _finddata_t fileinfo; //文件信息的结构体
handle = _findfirst(to_search,&fileinfo); //第一次查找
if (-1 == handle)return -1;
if (fileinfo.attrib != _A_SUBDIR)//令文件的属性不属于子文件夹
printf("%s\n", fileinfo.name);
ofstream outdata; //定义输出流
outdata.open("E:\\list.txt", ios::app);//ios::app是尾部追加的意思
outdata << fileinfo.name << endl; //打印出找到的文件的文件名
while (!_findnext(handle, &fileinfo)) //循环查找其他符合的文件,知道找不到其他的为止
{
if (fileinfo.attrib != _A_SUBDIR)//
printf("%s\n", fileinfo.name);
//outdata.open("E:\\list.txt", ios::app);//ios::app是尾部追加的意思
outdata << fileinfo.name << endl;
}
outdata.close(); //循环结束关闭输出流
_findclose(handle); //别忘了关闭句柄
system("pause");
return 0;
}
【2】Read strings line by line from TXT
There are two main ways to read. Delimiters such as /t and /n can be recognized internally.
#include<iostream>
#include<fstream>
#include <io.h>
#include<string> //通过>>将流析取器传入字符串要用到该文件
using namespace std;
const char *to_search = "E:\\opencv学习\\学习opencv源码\\official-code-in-the-learning-Opencv3-master\\calibration\\*.jpg";//欲查找的文件,支持通配符(可选择文件类型)
//可以指定查找所有文件\\*.*,也可以是指定文件\\*.格式类型
int main()
{
ifstream ifile;
ifile.open("E:\\list.txt", ios::in );
if (!ifile)
{
cout << "文件为打开" << endl;
}
//第一种方法读取
//string str; //将str改为char类型,可以按字符读取
输入文件流
//while (ifile >> str)
//{
//
// cout << str << endl;
//}
//第二种方法
while (!ifile.eof()) //判断是否到txt截止符
{
string str;
getline(ifile,str);
cout << str << endl;
}
ifile.close();//不要忘记运行结束后关闭传入流
system("pause");
return 0;
}
关于传入传出流是相对于VS操作而言,从外部到VS是传入,从vs到外部为传出。