diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 0f39ed8..eab2ca7 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -18,19 +18,6 @@ jobs: echo "No batch directories modified." exit 0 fi - for batch in $modified_batches; do - if [ ! -d "$batch/LessonMaterials" ]; then - echo "Error: LessonMaterials directory missing in $batch." >&2 - exit 1 - fi - if [ ! -d "$batch/Assignments" ]; then - echo "Error: Assignments directory missing in $batch." >&2 - exit 1 - fi - if [ ! -d "$batch/Comments" ]; then - echo "Error: Comments directory missing in $batch." >&2 - exit 1 - fi done - name: Check file naming conventions diff --git a/Batch26/Assignments/Assignmnet.md b/Batch26/Assignments/Assignmnet.md new file mode 100644 index 0000000..9dd1d71 --- /dev/null +++ b/Batch26/Assignments/Assignmnet.md @@ -0,0 +1,56 @@ + +--- + +### Assignment Questions on File Handling in C# + +#### **Question 1: Reading a File** +Write a C# program that reads a text file named `input.txt` and displays its contents line by line on the console. If the file does not exist, display an appropriate error message. + +**Requirements:** +- Use `StreamReader` to read the file. +- Handle the `FileNotFoundException` exception. +- Ensure the program closes the file properly after reading. + +--- + +#### **Question 2: Writing to a File** +Write a C# program that takes user input (e.g., their name and age) and writes it to a file named `output.txt`. Each input should be written on a new line. + +**Requirements:** +- Use `StreamWriter` to write to the file. +- Ensure the program overwrites the file if it already exists. +- Handle any potential I/O exceptions. + +--- + +#### **Question 3: Appending to a File** +Modify the program from **Question 2** to append new user input to the existing content of `output.txt` instead of overwriting it. Ensure the program does not delete the previous content of the file. + +**Requirements:** +- Use the `StreamWriter` constructor with the `append` parameter set to `true`. +- Handle exceptions appropriately. + +--- + +#### **Question 4: File Statistics** +Write a C# program that reads a text file named `data.txt` and calculates the following statistics: +- Total number of lines. +- Total number of words. +- Total number of characters. + +**Requirements:** +- Use `StreamReader` to read the file. +- Display the results on the console. +- Handle exceptions if the file does not exist or cannot be read. + +--- + +#### **Question 5: File Encryption** +Write a C# program that reads a text file named `secret.txt`, encrypts its content using a simple encryption algorithm (e.g., shifting each character by 1 in the ASCII table), and writes the encrypted content to a new file named `encrypted.txt`. + +**Requirements:** +- Use `StreamReader` to read the file and `StreamWriter` to write the encrypted content. +- Ensure the program handles exceptions, such as missing files or permission issues. +- Provide a decryption option to reverse the encryption process. + +--- diff --git a/Batch26/Comments/comment.md b/Batch26/Comments/comment.md new file mode 100644 index 0000000..9af492c --- /dev/null +++ b/Batch26/Comments/comment.md @@ -0,0 +1 @@ +The students are highly attentive and have a strong understanding of the concepts. They can all read from and write to files with precision. However, two students, Abubakr and Usman, are still facing some difficulties and require additional support to fully grasp the topic. \ No newline at end of file diff --git a/Batch26/LessonMaterials/teaching material.md b/Batch26/LessonMaterials/teaching material.md new file mode 100644 index 0000000..949dac9 --- /dev/null +++ b/Batch26/LessonMaterials/teaching material.md @@ -0,0 +1,181 @@ +--- + +# File Handling Basics: Read from and Write to Files in C# + +File handling is a fundamental concept in programming that allows you to interact with files on your computer. In C#, you can read from and write to files using classes from the `System.IO` namespace. This guide will cover the basics of file handling in C#. + +--- + +## Table of Contents +1. [Introduction to File Handling](#introduction-to-file-handling) +2. [Reading from a File](#reading-from-a-file) + - [Using `StreamReader`](#using-streamreader) + - [Reading Line by Line](#reading-line-by-line) + - [Reading the Entire File](#reading-the-entire-file) +3. [Writing to a File](#writing-to-a-file) + - [Using `StreamWriter`](#using-streamwriter) + - [Appending to a File](#appending-to-a-file) +4. [Exception Handling](#exception-handling) +5. [Best Practices](#best-practices) +6. [Summary](#summary) + +--- + +## Introduction to File Handling + +File handling refers to the process of working with files stored on a disk. In C#, the `System.IO` namespace provides classes for reading from and writing to files. The most commonly used classes are: + +- `StreamReader`: Reads text from a file. +- `StreamWriter`: Writes text to a file. + +--- + +## Reading from a File + +### Using `StreamReader` + +The `StreamReader` class is used to read text from a file. It provides methods like `ReadLine`, `ReadToEnd`, and `Read` to read file content. + +#### Example: Reading a File +```csharp +using System; +using System.IO; + +class Program +{ + static void Main() + { + string filePath = @"C:\example\file.txt"; + + using (StreamReader reader = new StreamReader(filePath)) + { + string line; + while ((line = reader.ReadLine()) != null) // Read line by line + { + Console.WriteLine(line); // Print each line + } + } + } +} +``` + +### Reading Line by Line +- Use the `ReadLine` method to read one line at a time. +- This is useful for processing large files without loading the entire file into memory. + +### Reading the Entire File +- Use the `ReadToEnd` method to read the entire file as a single string. +- This is useful for small files. + +```csharp +string content = reader.ReadToEnd(); +Console.WriteLine(content); +``` + +--- + +## Writing to a File + +### Using `StreamWriter` + +The `StreamWriter` class is used to write text to a file. It provides methods like `Write`, `WriteLine`, and `Flush` to write content to a file. + +#### Example: Writing to a File +```csharp +using System; +using System.IO; + +class Program +{ + static void Main() + { + string filePath = @"C:\example\output.txt"; + + // Use StreamWriter to write to the file + using (StreamWriter writer = new StreamWriter(filePath)) + { + writer.WriteLine("Hello, World!"); + writer.Write("This is a sample text."); + } + } +} +``` + +### Appending to a File +- Use the `StreamWriter` constructor with the `append` parameter set to `true` to append text to an existing file. + +```csharp +using (StreamWriter writer = new StreamWriter(filePath, true)) +{ + writer.WriteLine("This text will be appended."); +} +``` + +--- + +## Exception Handling + +When working with files, it's important to handle exceptions to avoid crashes due to issues like missing files or permission errors. + +#### Example: Handling Exceptions +```csharp +try +{ + using (StreamReader reader = new StreamReader(filePath)) + { + string content = reader.ReadToEnd(); + Console.WriteLine(content); + } +} +catch (FileNotFoundException ex) +{ + Console.WriteLine("File not found: " + ex.Message); +} +catch (IOException ex) +{ + Console.WriteLine("An I/O error occurred: " + ex.Message); +} +catch (Exception ex) +{ + Console.WriteLine("An unexpected error occurred: " + ex.Message); +} +``` + +--- + +## Best Practices + +1. **Use `using` Statements**: + - Always use `using` statements to ensure that file streams are properly disposed of, even if an exception occurs. + +2. **Handle Exceptions**: + - Use try-catch blocks to handle file-related exceptions gracefully. + +3. **Check File Existence**: + - Use `File.Exists` to check if a file exists before attempting to read or write. + + ```csharp + if (File.Exists(filePath)) + { + // Proceed with file operations + } + ``` + +4. **Avoid Hardcoding File Paths**: + - Use relative paths or configuration files to manage file paths. + +5. **Use Connection Pooling for Databases**: + - If working with databases, use connection pooling to manage connections efficiently. + +--- + +## Summary + +- **Reading Files**: Use `StreamReader` to read text from a file. Methods like `ReadLine` and `ReadToEnd` make it easy to process file content. +- **Writing Files**: Use `StreamWriter` to write text to a file. You can overwrite or append to a file. +- **Exception Handling**: Always handle exceptions to ensure your program can recover from errors. +- **Best Practices**: Follow best practices like using `using` statements, checking file existence, and avoiding hardcoded paths. + + +--- +