25/26jan2021. VEŽBE -> Čitanje teksta iz fajla -> StreamReader - Youtube snimci
Completion requirements
Prvi deo: čitanje TEKSTA iz fajla
Kod:
using System;
using System.IO; //potrebno za StreamReader
using System.Windows.Forms;
namespace ConsoleApp7
{
class Program
{
[STAThread]
static void Main(string[] args)
{
var ofd = new OpenFileDialog();
ofd.Filter = "Textualni fajlovi (*.txt) |*.txt";
if(ofd.ShowDialog()==DialogResult.OK)
{
Console.ReadKey();
Console.Clear();
try
{
using (var sr = new StreamReader(ofd.FileName)) // koristimo streamreadre dok ne zavrsimo i automatski se izbacuje iz memorije
{
string linijaTeksta;
while ((linijaTeksta = sr.ReadLine()) != null) //citamo liniju po liniju i ispisujemo u konzolu dok ne dodjemo do kraja fajla
{
Console.WriteLine(linijaTeksta);
}
}
}
catch(FileNotFoundException fileEx)
{
Console.WriteLine("Fajl nije pronadjen, molimo proverite opet");
Console.WriteLine("Lokacija fajla: " + fileEx.FileName);
}
catch(Exception ex)
{
Console.WriteLine("GRESKA: ");
Console.WriteLine(ex.ToString());
}
}
}
}
}
**************************************
drugi deo:
Drugi deo: čitanje BAJTOVA iz fajla
treći deo (OPCIONO):
PEEK vs. READ
Last modified: Sunday, 31 January 2021, 10:32 PM