C ++ reads all txt files from a folder
Some time ago, to create a project, you need to read all the txt files in a folder. After querying the information, you can obtain the following implementation methods:
First, let's take a look at this struct.
Struct _ finddata_t {
Unsigned attrib;
Time_t time_create;
Time_t time_access;
Time_t time_write;
_ Fsize_t size;
Char name [260];
};
The meanings of member variables are as follows:
Unsigned atrrib: The storage location of the file attribute. It stores an unsigned unit to indicate the attributes of a file. File attributes are represented in bits, mainly including _ A_ARCH (archive), _ A_HIDDEN (hide), _ A_NORMAL (normal), and _ A_RDONLY (read-only), _ A_SUBDIR (folder), _ A_SYSTEM (system ). These are all macro defined in the middle, which can be used directly, and its meaning is actually an unsigned integer (but this integer should be several power of 2, so that only one digit is 1, and the other digit is 0 ). Since it is a bit representation, when a file has multiple attributes, it often obtains the synthesis of several attributes by bit or. For example, the read-only + hide + system attribute should be: _ A_HIDDEN | _ A_RDONLY | _ A_SYSTEM.
Time_t time_create: File Creation Time.
Time_t time_access: The last time the file was accessed.
Time_t time_write: The last time the file was modified.
_ Fsize_t size: file size.
Char name [_ MAX_FNAME]: file name. Here _ MAX_FNAME is a constant macro, which is defined in the header file, indicating the maximum length of the file name.
The _ findfirst and _ findnext functions are required for file search. These two functions are included in the io. h library.
1. _ findfirst function: long _ findfirst (const char *, struct _ finddata_t *);
The first parameter is the file name. You can use "*. *" to find all files, or "*. cpp" to find the. cpp file. The second parameter is the _ finddata_t struct pointer. If the search is successful, the file handle is returned. If the search fails,-1 is returned.
2. _ findnext function: int _ findnext (long, struct _ finddata_t *);
The first parameter is the file handle, and the second parameter is also the _ finddata_t struct pointer. If the search is successful, 0 is returned. If the search fails,-1 is returned.
3. _ findclose () function: int _ findclose (long );
There is only one parameter, the file handle. If the function is disabled successfully, 0 is returned. If the function fails,-1 is returned.
Code and implementation
File to be output
Running result
Code
# Include <iostream>
# Include <string>
# Include <fstream>
# Include <io. h>
Using namespace std
Void GetLineAndPrint (string in_name)
{
Ifstream fin (in_name );
If (! Fin)
{
Cerr <"open file error" <endl;
Exit (-1 );
}
String str;
While (getline (fin, str ))
{
Cout <str <endl;
}
}
Int main ()
{
Struct _ finddata_t fileinfo;
String in_path;
String in_name;
Cout <"Enter the folder path :";
Cin> in_path;
String curr = in_path + "\ *. txt ";
Long handle;
If (handle = _ findfirst (curr. c_str (), & fileinfo) =-1L)
{
Cout <"no matching file found! "<Endl;
Return 0;
}
Else
{
In_name = in_path + "\" + fileinfo. name;
GetLineAndPrint (in_name );
While (! (_ Findnext (handle, & fileinfo )))
{
In_name = in_path + "\" + fileinfo. name;
GetLineAndPrint (in_name );
}
_ Findclose (handle );
}
}
Note: The code is compiled in vs2017.
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151330.htm