Tell me how to read the file and display it on the label component? Here is the code that I’ve got now, but it always displays (null) .

 - (IBAction)listenText:(id)sender { NSFileHandle *file; NSData *databuffer; file = [NSFileHandle fileHandleForReadingAtPath: @"/Users/admin/text.txt.txt"]; if (file == nil) { NSLog(@"Failed to open file"); } NSString* s = [NSString stringWithFormat:@"%@", file]; _labelText.stringValue = s; [file closeFile]; } 
  • you understand that your file is an instance of the NSFileHandler class and you still have to do something with it to read the data from the file. Start by reading the developer.apple.com/reference/foundation/nsfilehandle documentation - Max Mikheyenko
  • Thank you very much for the information provided. It really helped. From now on I will immediately read the documentation. - user220343

2 answers 2

Decision:

 - (IBAction)listenText:(id)sender { NSFileHandle *file; NSData *databuffer; file = [NSFileHandle fileHandleForReadingAtPath: @"/Users/admin/text.txt"]; databuffer = [file readDataToEndOfFile]; NSLog(@"%@", [NSString stringWithContentsOfFile: @"/Users/admin/text.txt" encoding:NSUTF8StringEncoding error:NULL]); NSString* s = [NSString stringWithContentsOfFile: @"/Users/admin/text.txt" encoding:NSUTF8StringEncoding error:NULL]; _labelText.stringValue = s; [file closeFile]; } 

    There is a very simple way to read a file:

     NSData *fileData = [[NSData alloc] dataWithContentsOfFile:@"/Users/admin/text.txt"]; NSString *fileString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];