The number of lines in iOS that read all the text in a folder

Source: Internet
Author: User

#import<Foundation/Foundation.h>intCodelinecount (NSString *path) {      //1. Get the File ManagerNsfilemanager *mgr =[Nsfilemanager Defaultmanager]; //determine if path is a folder or a file pathBOOL dir = NO;//whether the tag is a folder//whether this path existsBOOL exist = [Mgr Fileexistsatpath:path isdirectory:&dir]; //3. If there is no    if(!exist)return 0; //4 path exists    if(dir) {//NSLog (@ "is a folder"); //Contains all the contents (folders, files) under the current folderNsarray *array =[Mgr Contentsofdirectoryatpath:path Error:nil]; intCount =0; //iterate through all the file names in the array         for(NSString *filenameinchArray) {            //full path to sub-file (clip)NSString *fullpath = [NSString stringWithFormat:@"%@/%@", Path,filename]; //Loop CallCount + =Codelinecount (FullPath); }        returncount; }    Else    {        //to determine the file name extensionNSString *extension =[[Path pathextension] lowercasestring]; if(! ([Extension isequaltostring:@"h"]             || [Extension isequaltostring:@"m"]             || [Extension isequaltostring:@"C"])           ){            //the file extension is not H, and is not M, and is not a C            return 0; }                //file//1. Loading file contentsNSString *content =[NSString Stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil]; //2. Separate the contents of the file into each row//create a character array by \ n DelimitedNsarray *array = [content componentsseparatedbystring:@"\ n"]; NSString*STR = [path stringbyreplacingoccurrencesofstring:@"/users/laouhn/documents/project"Withstring:@""]; NSLog (@"%@-%ld", Str,array.count); return(int) Array.count; }}intMainintargcConst Char*argv[]) {    intCount = Codelinecount (@"/users/laouhn/documents/project"); NSLog (@"%d", Count); return 0;}//void Test ()//{//nsstring *str = @ "Jack\nrose\njim\njake";//    //write something to the file//[str writetofile:@ "/users/laouhn/desktop/crayons.txt" Atomically:yes encoding:nsutf8stringencoding Error:nil]; //    //Delimited by character//nsarray *array = [str componentsseparatedbystring:@ "\ n"];//    //    //}//

The number of lines in iOS that read all the text in a folder

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.