C ++ reads all txt files from a folder and txt files

Source: Internet
Author: User

C ++ reads all txt files from a folder and txt files

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.

 

References:

Http://blog.csdn.net/black__blood/article/details/7168740

Http://www.cnblogs.com/ranjiewen/p/5960976.html#top

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.