17th: Calculate Code line number practice code

Source: Internet
Author: User

Use some of the classes in the foundation:

Nsmanager

NSString

Nsarray

main.m//10-Calculates the number of lines of code////Created by xxx on 15/9/16.//Copyright (c) 2015 Itcast. All rights reserved.//#import <foundation/foundation.h>int countlineoffile (NSString * path) {//Get File Manager nsfile    Manager * Manager = [Nsfilemanager Defaultmanager];    Determine if path is not a computable file path BOOL isfile = NO;    BOOL exist = [Manager Fileexistsatpath:path isdirectory:&isfile];    if (!exist) {//path file path does not exist with return 0; }//If path exists if (isfile) {//path is folder//get all contents under current folder: folder and file Nsarray * Filearray = [Manager cont        Entsofdirectoryatpath:path Error:nil];        int count = 0; View all files and folders for (NSString * file in Filearray) {count + = Countlineoffile ([NSString stringWithFormat        : @ "%@/%@", Path,file]);    } return count;        } else{//Get file extension nsstring * extension = [path pathextension]; if (! ( [Extension isequaltostring:@ "h"] | |           [Extension isequaltostring:@ "M"]  || [Extension isequaltostring:@ "C"] | |            [Extension isequaltostring:@ "CPP"])        return 0; Get all contents of file path to path file NSString * Fileconten = [NSString stringwithcontentsoffile:path Encoding:nsutf8stringencodin        G Error:nil];        A string is divided into multiple strings with ' \ n ', that is, each line as a string Nsarray * array = [Fileconten componentsseparatedbystring:@ "\ n"];                for (NSString * str in array)//NSLog (@ "%@", str);    return (int) Array.count; }}int Main (int argc, const char * argv[]) {//count the number of lines of code in a file or folder, including empty lines int count = Countlineoffile (@ "/users/qujie/    DOCUMENTS/OC program/10-Calculate the number of lines of code ");    NSLog (@ "Count =%d", count); NSString * str = @ "123-456-789";//Nsarray * array = [str componentsseparatedbystring:@ "-"];//NSLog (@ "%@", arr    AY); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

17th: Calculate Code line number practice code

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.