Concept Flow - Reading text files
Start
Open file
Read line or all text
Process or display text
Close file
End
The program opens a text file, reads its content line by line or all at once, processes or shows the text, then closes the file.
using System; using System.IO; class Program { static void Main() { string text = File.ReadAllText("example.txt"); Console.WriteLine(text); } }
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Start program | N/A | Program begins |
| 2 | Call File.ReadAllText("example.txt") | File exists and readable | Reads entire file content into 'text' |
| 3 | Store content in variable 'text' | Content stored | Variable 'text' holds file text |
| 4 | Call Console.WriteLine(text) | Print text to console | Text appears on screen |
| 5 | Program ends | N/A | Program stops running |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| text | null | "(file content)" | "(file content)" | "(file content)" |
Reading text files in C#:
- Use File.ReadAllText("filename") to read whole file as string.
- Use StreamReader to read line by line for large files.
- Always ensure file exists to avoid errors.
- Print content with Console.WriteLine.
- Close files automatically with using or rely on File methods.