---
title: "stringWithContentsOfFile:encoding:error:"
framework: foundation
role: symbol
role_heading: Type Method
path: "foundation/nsstring/stringwithcontentsoffile:encoding:error:"
---

# stringWithContentsOfFile:encoding:error:

Returns a string created by reading data from the file at a given path interpreted using a given encoding.

## Declaration

```occ
+ (instancetype) stringWithContentsOfFile:(NSString *) path encoding:(NSStringEncoding) enc error:(NSError **) error;
```

## Parameters

- `path`: A path to a file.
- `enc`: The encoding of the file at path. For possible values, see doc://com.apple.foundation/documentation/Foundation/NSStringEncoding.
- `error`: If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.

## Return Value

Return Value A string created by reading data from the file named by path using the encoding, enc. If the file can’t be opened or there is an encoding error, returns nil.

## See Also

### Creating and Initializing a String from a File

- [init(contentsOfFile:encoding:)](foundation/nsstring/init(contentsoffile:encoding:).md)
- [stringWithContentsOfFile:usedEncoding:error:](foundation/nsstring/stringwithcontentsoffile:usedencoding:error:.md)
- [init(contentsOfFile:usedEncoding:)](foundation/nsstring/init(contentsoffile:usedencoding:).md)
