2022.01.14.-17. Fájlkezelés - egy sorban több adat

Fájlkezelés - egy sorban több adat

Mintaprogram:


Feladat: Vezeték és keresztneveket tartalmazó fájlt olvassunk be és írjunk ki. A neveket két tömbben tároljuk.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

 

namespace fajlKezelesBonyolultabb

{

    class Program

    {   

        static string[] nevek = new string[8];

        static int[] korok = new int[8];

 

 

        static void kiir()

        {

            for (int i = 0; i < nevek.Length; i++)

            {

                 Console.WriteLine("{0}, {1}",nevek[i], korok[i]);

            }

        }

 

        static void Main(string[] args)

        {

            StreamReader sr = new StreamReader("diakok.txt");

            string[] atmeneti;

            int i = 0;

            while (!sr.EndOfStream)

            {

                atmeneti = sr.ReadLine().Split(',');

                nevek[i] = atmeneti[0];

                korok[i] = Convert.ToInt32(atmeneti[1]);

                i++;

            }

            sr.Close();

            kiir();

 

            StreamWriter nagybetus = new StreamWriter("nagy.txt");

            for (int j = 0; j < korok.Length; j++)

            {

                nagybetus.WriteLine(nevek[j].ToUpper());

            }

            nagybetus.Close();

 

            Console.ReadLine();

        }

    }

}

Teszt fájl:

Öröm Melitta,15

Avon Mór,16

Ordy Bálint,17

Szikla Szilárd,15

Bekre Pál,17

Heu Réka,17

Mérta Nóra,18

Emanci Pál,16



2022.01.12. Fájlkezelés gyakorlás

 Írtunk önállóan egy fájlkezelést, ami egy 100 elemű tömböt kezel. Kiírtuk fájlba 100-szor a 100 elemű tömböt.


using System;
using System.IO;



namespace _2022._01._12_
{
  class Program
  {
    static int[] tomb1 = new int[100];
    static void kiir()
    {
      for (int i = 0; i < tomb1.Length; i++)
      {
        Console.WriteLine(tomb1[i]);

      }

    }
    static void Main(string[] args)
    {

      StreamReader sr = new StreamReader("szamok.txt");
      int i = 0;
      while (!sr.EndOfStream)
      {
        tomb1[i] = Convert.ToInt32(sr.ReadLine());
        i++;

      }

      sr.Close();

      kiir();
      Console.ReadLine();

      StreamWriter sw = new StreamWriter("negyzetgyok.txt");
      i = 0;
      while (i < tomb1.Length)
      {

        sw.WriteLine(Math.Sqrt(tomb1[i]));
        i++;
      }
      sw.Close();
      Console.ReadLine();

      StreamWriter sw1 = new StreamWriter("nagy.txt");
      i = 0;
      int j = 0;
      int szamlalo = 1;
      while (i < 100)
      {
        j = 0;
        while (j < 100)
        {
          sw1.WriteLine("{0}. {1}", szamlalo, tomb1[j]);
          szamlalo++;
          j++;
        }
        i++;
      }
      sw1.Close();

    }
  }
}


2021.01.05. , 07. , 10. Fájlkezelés bevezetés

Házi feladat otthon működtetni a programot.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO; //ezt pótolni kell!

 

namespace fajlkezeles

{

    class Program

    {

        static int[] szamTomb = new int[10];

 

        static void kiiras()

        {

            for (int i = 0; i < szamTomb.Length; i++)

            {

                Console.WriteLine(szamTomb[i]);

            }

        }

 

        static void Main(string[] args)

        {

            StreamReader sr = new StreamReader("szamok.txt"); //olvasásra megnyitjuk a fájlt

            int i = 0;

            while (!sr.EndOfStream) //addig megyünk, míg vége nincs a fájlnak

            {

                szamTomb[i]= Convert.ToInt32(sr.ReadLine()); //egyszerre egy sort beolvasunk, konvertálunk és beteszünk a tömbbe

                i++;

            }

            sr.Close(); //lezárjuk a fájlt: KÖTELEZŐ ELEM!

 

            kiiras();

 

            StreamWriter sw = new StreamWriter("dupla.txt"); //írásra megnyitunk egy fájlt; ha eddig nem létezett, létre is hozza

            for (int j = 0; j < szamTomb.Length; j++)

            {

                sw.WriteLine(szamTomb[j]*2); //írunk a fájlba soronként

            }

            sw.Close(); //lezárjuk a fájlt: KÖTELEZŐ ELEM!

 

            Console.ReadLine();

        }

    }

}

2022.01.05. Struktúra - ismétlés


namespace struktura

{

    class Program

    {

        public struct auto

        {

            public string gyarto;

            public double motor; //köbcenti

            public int ulesek; //ülések száma

            public bool akcio; //akciós-e

        }

 

        static auto[] autok = new auto[3];

 

        static void bekeres()

        {

            for (int i = 0; i < autok.Length; i++)

            {

                Console.Write("Kérem az {0}. autó gyártóját: ",i+1);

                autok[i].gyarto = Console.ReadLine();

                Console.Write("Kérem az {0}. autó motor térfogatát: ",i+1);

                autok[i].motor = Convert.ToDouble(Console.ReadLine());

                Console.Write("Kérem az {0}. autó üléseinek számát: ",i+1);

                autok[i].ulesek = Convert.ToInt32(Console.ReadLine());

                Console.Write("Adja meg, hogy a(z) {0}. autó akciós-e (true/false)?: ",i+1);

                autok[i].akcio = Convert.ToBoolean(Console.ReadLine());

            }

        }

 

        static void kiiras()

        {

            for (int i = 0; i < autok.Length; i++)

            {

                Console.WriteLine("{0} autó", i+1);

                Console.WriteLine(autok[i].gyarto);

                Console.WriteLine(autok[i].motor);

                Console.WriteLine(autok[i].ulesek);

                Console.WriteLine(autok[i].akcio);

                Console.WriteLine();

            }

        }

 

        static void Main(string[] args)

        {

            bekeres();

            kiiras();

            Console.ReadLine();

        }

    }

}


2023.04.26. Javító feladatsor