Read Text File In C#

File I/O in C is very similar to Matlab. There are two main differences. One, we have to type the FILE variable. Two, we read one value (or a single line of values) at a time, whereas by default in Matlab, you may read many values at once.

The basic steps for using a File in C are always the same:

From this video you will find how to open and read text file using C programming language.SUBSCRIBE FOR MORE:http://www. To read contents of text file character by character in C programming, follow these steps. Open the text file in read mode, using fopen. If file pointer is null, return 1, else continue. Using fgetc function, read next character from text file into a string. Fgetc returns character read from the file. If the file is found, the program saves the content of the file to a string c until ' ' newline is encountered. Suppose the program.txt file contains the following text in the current directory.

  1. Create a variable of type 'FILE*'.

  2. Open the file using the 'fopen' function and assign the 'file' to the variable.

  3. Check to make sure the file was successfully opened by checking to see if the variable NULL. If it does, an error has occured.

  4. Use the fprintf or fscanf functions to write/read from the file. Usually these function calls are placed in a loop. In the case of reading data, usually, the data is read in and placed in an array, but sometimes we process the data 'on the fly' (i.e., we do not store the data, we process it and create a result directly before reading any more data.

Example Code

Here are examples of the basic syntax for opening a file and writing to or reading from it:

Here is a comparison between reading from a file in Matlab and in C:

Matlab

FGETS function: Read One Line at a Time

To read one line from a file (or the keyboard) at a time, use the fgets function.

fgets places the 'n' (newline) at the end of the line. Thus if we type in 'hello', what really goes into the variable line is [ 'h', 'e', 'l', 'l', 'o', 'n', '0' ]

fgets returns the keyword null on error. Thus we often use:

-->

This example reads the contents of a text file by using the static methods ReadAllText and ReadAllLines from the System.IO.File class.

For an example that uses StreamReader, see How to read a text file one line at a time.

Note

The files that are used in this example are created in the topic How to write to a text file.

Read Text File In C# Line By Line

Example

Read text file in c# wpf

Compiling the Code

Copy the code and paste it into a C# console application.

If you are not using the text files from How to write to a text file, replace the argument to ReadAllText and ReadAllLines with the appropriate path and file name on your computer.

Robust Programming

The following conditions may cause an exception:

  • The file doesn't exist or doesn't exist at the specified location. Check the path and the spelling of the file name.
Read Text File In C#

.NET Security

Read

Do not rely on the name of a file to determine the contents of the file. For example, the file myFile.cs might not be a C# source file.

Read Text File In C#

Read Text File In C#

See also