Skip to content

Commit

Permalink
Add W6 D1 programs
Browse files Browse the repository at this point in the history
  • Loading branch information
cvpfus committed Oct 9, 2023
1 parent afa84c3 commit 411a355
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 0 deletions.
16 changes: 16 additions & 0 deletions FormulatrixBootcamp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W5 D5", "W5 D5", "{A966AD2E
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PreprocessingDirectives", "W5 D5\PreprocessingDirectives\PreprocessingDirectives.csproj", "{0AA3960D-7152-4FEE-B125-3C1E18FBE395}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W6 D1", "W6 D1", "{BC48B9D4-ECEE-4451-B974-453C56155A59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileStream", "W6 D1\FileStream\FileStream.csproj", "{39C38AE1-9B7A-4957-9C49-952A96ACF06A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamWriterAndStreamReader", "W6 D1\StreamWriterAndStreamReader\StreamWriterAndStreamReader.csproj", "{DE7BEB38-C495-41B5-A5F2-71691E61C55B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -383,11 +389,21 @@ Global
{0AA3960D-7152-4FEE-B125-3C1E18FBE395}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AA3960D-7152-4FEE-B125-3C1E18FBE395}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AA3960D-7152-4FEE-B125-3C1E18FBE395}.Release|Any CPU.Build.0 = Release|Any CPU
{39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Release|Any CPU.Build.0 = Release|Any CPU
{DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DE7BEB38-C495-41B5-A5F2-71691E61C55B} = {BC48B9D4-ECEE-4451-B974-453C56155A59}
{39C38AE1-9B7A-4957-9C49-952A96ACF06A} = {BC48B9D4-ECEE-4451-B974-453C56155A59}
{0AA3960D-7152-4FEE-B125-3C1E18FBE395} = {A966AD2E-80DB-49E0-8070-B6DFE0C1E36D}
{09D51696-9994-41B5-9959-9DFD1B331B02} = {5637E846-0989-4572-83A1-6CBC066E2D2D}
{CCE2C0C8-CD08-44DA-AD87-32848B8B64A7} = {FC559EAF-E754-4775-A039-64B804F331A8}
Expand Down
10 changes: 10 additions & 0 deletions W6 D1/FileStream/FileStream.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions W6 D1/FileStream/MyString.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
1 change: 1 addition & 0 deletions W6 D1/FileStream/MyString2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Formulatrix Indonesia
1 change: 1 addition & 0 deletions W6 D1/FileStream/MyString3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Formulatrix Bootcamp 5
36 changes: 36 additions & 0 deletions W6 D1/FileStream/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text;
using System.IO;

public class Program
{
static void Main()
{
int offset = 0;

// Write to a file
string myString = "Hello World";
using(FileStream fs = new(@"./MyString.txt", FileMode.Create, FileAccess.Write)) {
byte[] bytes = Encoding.UTF8.GetBytes(myString);
fs.Write(bytes, offset, bytes.Length - offset);
}

// Read from a file
using(FileStream fs = new(@"./MyString2.txt", FileMode.Open, FileAccess.Read))
{
Console.WriteLine(fs.Length);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, offset, bytes.Length - offset);
string myString2 = Encoding.UTF8.GetString(bytes);
Console.WriteLine(myString2);
}

// FileShare
using(FileStream fs = new(@"./MyString3.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
while (true)
{

}
}
}
}
54 changes: 54 additions & 0 deletions W6 D1/StreamWriterAndStreamReader/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public class Program
{
static void Main()
{
string file1 = @"./Text1.txt";
string file2 = @"./Text2.txt";
string file3 = @"./Text3.txt";

using (FileStream fs = new(file1, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (StreamWriter sw = new(fs))
{
sw.BaseStream.Position = fs.Seek(0, SeekOrigin.End);
sw.WriteLine();
sw.WriteLine("This is new line");
}
}
string[] myString = File.ReadAllLines(file1);
foreach (var s in myString)
{
Console.WriteLine(s);
}

// Modify specific line
int line = 2;
string newString = "This is new line";
string[] myString2 = File.ReadAllLines(file2);
using (FileStream fs = new(file2, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (StreamWriter sw = new(fs))
{
myString2[line - 1] = newString;
foreach (var s in myString2)
{
sw.WriteLine(s);
}
}
}

// Async method
Task.Run(async () =>
{
using (FileStream fs = new(file3, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (StreamWriter sw = new(fs))
{
sw.BaseStream.Position = fs.Seek(0, SeekOrigin.End);
await sw.WriteLineAsync("Hello");
await sw.WriteLineAsync("Hi");
}
}
}).Wait();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
4 changes: 4 additions & 0 deletions W6 D1/StreamWriterAndStreamReader/Text1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is line 1
This is line 2
This is line 3
This is line 4
4 changes: 4 additions & 0 deletions W6 D1/StreamWriterAndStreamReader/Text2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is line 1
This is new line
This is line 3
This is line 4
8 changes: 8 additions & 0 deletions W6 D1/StreamWriterAndStreamReader/Text3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Hello
Hi
Hello
Hi
Hello
Hi
Hello
Hi

0 comments on commit 411a355

Please sign in to comment.