Search code examples
iphoneiosios5file-uploadfwrite

How can calculate file data or records in iPhone?


I am using to write some logs to write in locally when logs or data count above 50 i want to post the data to server. Then i write the next 50 logs values. My question is how to get the file count and how can i maintain this any one help me.


Solution

  • EDIT count records inside the File

    Notice when addnew log to file add with \n line character so we can know how many logs added

    NSString *str = [[NSString alloc]initWithContentsOfFile:yourFilePath encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrLogs = [str componentsSeparatedByString:@"\n"];
    NSLog(@"Count of logs : %d",[arrLogs count]);
    

    check this to how to get count of contents like this:

    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];
    

    Now filter contents like this:

    NSPredicate *fltrtxt = [NSPredicate predicateWithFormat:@"self ENDSWITH '.txt'"];
    NSArray *onlytxts = [dirContents filteredArrayUsingPredicate:fltrtxt];
    

    Now count is:

    if([onlytxts count] >= 50)
    {
      //save new log to doc dir
      // post data to server;
      //after posting data u can do anything
    }