How to Read .txt File in C Xcode
How to create a file, write data into it and read data from it on iOS?
Existence a software developer nosotros should be always aware of how to play with files, write to a file, read from the file then on.
In this mail we are going to learn the same, nosotros will be creating a file and writing the information to the file and subsequently reading through the same file.
So let'due south get started,
Step one − Create new Xcode Project → Single View Application → name information technology "ReadingWritingFile"
Pace 2 − Open up ViewController.swift and add new function equally testify below
public func createAndWriteFile() { }
Now nosotros will create a file and will print the path of the file.
Footstep 3 − Inside createAndWriteFile function add together
permit fileName = "sample" permit documentDirectoryUrl = try! FileManager.default.url( for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true ) let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt") // prints the file path impress("File path \(fileUrl.path)")
At present your createAndWriteFile function should look like below one
public func createAndWriteFile() { let fileName = "sample" let documentDirectoryUrl = try! FileManager.default.url( for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true ) permit fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt") // prints the file path print("File path \(fileUrl.path)") //information to write in file. permit stringData = "Hi Tutorials Indicate" do { attempt stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8) } take hold of permit error equally NSError { print (fault) } }
Now nosotros will be writing to the file.
Add together below code to the existing function
//data to write in file. let stringData = "Hello Tutorials Point" do { try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8) } catch let error equally NSError { print (error) }
Step 4 − Your terminal function should await like below
// function to create file and write into the aforementioned. public func createAndWriteFile() { let fileName = "sample" let documentDirectoryUrl = effort! FileManager.default.url( for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true ) let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt") // prints the file path print("File path \(fileUrl.path)") //data to write in file. let stringData = "Howdy Tutorials Signal" do { try stringData.write(to: fileUrl, atomically: true, encoding: Cord.Encoding.utf8) } take hold of let error as NSError { print (error) } }
Step 5 − Run the project by calling the new method from viewDidLoad() and navigate the file path and validate the content.
Step 6 − Now we're going to read the content, copy the below code in the same part
var readFile = "" do { readFile = try String(contentsOf: fileUrl) } grab allow error as NSError { print(mistake) } print (readFile)
And you're done,
Step 7 − Complete code,
import UIKit form ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup later loading the view, typically from a nib. self.createReadAndWriteFile() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that tin be recreated. } // role to create file and write into the same. public func createReadAndWriteFile() { let fileName = "sample" let documentDirectoryUrl = try! FileManager.default.url( for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: truthful ) allow fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt") // prints the file path print("File path \(fileUrl.path)") //data to write in file. let stringData = "Hello Tutorials Signal." practise { try stringData.write(to: fileUrl, atomically: truthful, encoding: String.Encoding.utf8) } catch let error as NSError { impress (mistake) } var readFile = "" do { readFile = effort String(contentsOf: fileUrl) } catch permit error every bit NSError { print(error) } print (readFile) } }
Published on 16-Dec-2019 12:21:25
- Related Questions & Answers
- Write a Python lawmaking to read JSON data from a file and catechumen it to dataframe, CSV files
- Write a C program to read a data from file and display
- What is JDBC Blob data type? how to shop and read data from it?
- What is JDBC Clob information type? how to store and read data from it?
- How to read/write data from/to .properties file in Coffee?
- How to put a Pandas DataFrame into a JSON file and read information technology once again?
- Coffee Program to create a file and sets it to read-only
- How to read data from a file using FileInputStream?
- How to read information from PDF file and display on console in Coffee?
- How to read the information from a file in Java?
- Write a Python program to read an Excel data from file and read all rows of offset and last columns
- Read Data from a Text File using C++
- How to read information from *.CSV file using JavaScript?
- How to read data from .csv file in Java?
- How to read data from ane file and impress to some other file in Java?
Source: https://www.tutorialspoint.com/how-to-create-a-file-write-data-into-it-and-read-data-from-it-on-ios
0 Response to "How to Read .txt File in C Xcode"
Post a Comment