The number of rows (or number of words) of the small program statistics file (file in the folder) written by the foundation framework)

Source: Internet
Author: User
//// Main. M // calculate the number of lines of code /// created by Apple on 13-8-12. // copyright (c) 2013 apple. all rights reserved. // # import <Foundation/Foundation. h> // use the nsfilemanger, nsstring, and nsarray classes // calculate the number of lines of code in a single file (the file may be a folder) // path: the full path returned value of the file: intint codelinecount (nsstring * path) {// nsfilemanger is a single object nsfilemanager * Mgr = [nsfilemanager defaultmanager]; // determines whether the path is a file or a folder bool dir = no; // mark as a folder // determine whether the path has B OOl exist = [Mgr fileexistsatpath: path isdirectory: & dir]; // if not, 0 if (! Exist) {nslog (@ "this path does not exist"); Return 0 ;}if (DIR) {nslog (@ "Yes folder"); nsarray * array = [Mgr contentsofdirectoryatpath: path error: Nil]; // nslog (@ "% @", array); int sum = 0; For (nsstring * filename in array) {// full path nsstring * fullname = [nsstring stringwithformat: @ "% @/% @", path, filename]; sum + = codelinecount (fullname );} return sum;} else {nslog (@ "yes file"); // determine the file extension name nsstring * Extentsion = [[path pat Hextension] lowercasestring]; nslog (@ "% @", Extentsion); If (! [Extentsion isequaltostring: @ "H"] &! [Extentsion isequaltostring: @ "M"] &! [Extentsion isequaltostring: @ "C"] &! [Extentsion isequaltostring: @ "docx"]) {return 0;} // read the file (Load file content) nsstring * Contents = [nsstring stringwithcontentsoffile: path encoding: nsutf8stringencoding error: nil]; // nslog (@ "% @", contents); // use \ n to split the file nsarray * array = [contents componentsseparatedbystring: @ "\ n"]; // For (id obj in array) // {// nslog (@ "% @", OBJ); //} nsange range = [path rangeofstring: @ "users/Apple/desktop/"]; nsstring * Str = [Path stringbyreplacingcharactersinrange: range withstring: @ ""]; nslog (@ "% @ % ZD", STR, array. count); // return (INT) contents. length; Return (INT) array. count;} // The number of file shards is the number of file rows} int main (INT argc, const char * argv []) {// nsstring * STR = @ "Jak \ njdfs"; // [STR writetofile: @ "/users/Apple/desktop/data.txt" atomically: Yes encoding: nsutf8stringencoding error: nil]; int COUNT = codelinecount (@ "/E:/. RTF "); Nslog (@" the number of rows is ~~ % D ", count); Return 0 ;}

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.