Resolved record list doesn't take values from txt file, loop not running.

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
71
Programming Experience
Beginner
I try to read 'record data' from text file in a record list. unfortunately the loop not working and other things might not work, please help me, thank you, frank.

hotelorder1:
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace szalloda1
{
    public class pitypang00
    {
        public int foglaloszam = 0;
        public int szobaszam = 0;
        public int erkezesinapsorszama = 0;
        public int tavozasinapsorszam = 0;
        public int vendegszam = 0;
        public int kernekreggelit = 0;
        public string vendegazonosito = " ";

    }
    class Program
    {
        

        static void Main(string[] args)
        {
            string olvas = @"c:\Users\Public\textfiles\pitypang.txt";
            List<pitypang00> adatok = new List<pitypang00>();
            int i = 0;
            int i2 = 0;
            int kod0 = 0;
            int utolso = 0;
            try
            {
                
                // Open the text file using a stream reader.
                using (var sr = new StreamReader(olvas, Encoding.Default))
                {
                    // Read the stream as a string, and write the string to the console.
                    //Console.WriteLine(sr.ReadToEnd());
                    while (!sr.EndOfStream)
                    {

                       
 string sor = sr.ReadLine();
                        string[] record = sor.Split(' ');

                        adatok.Add(new pitypang00());
                        if (i == 0) { kod0 = Convert.ToInt32(record[0]); }
                        else
                        {
                            adatok[i].foglaloszam = Convert.ToInt32(record[1]);
                            adatok[i].szobaszam = Convert.ToInt32(record[2]);
                            adatok[i].erkezesinapsorszama = Convert.ToInt32(record[3]);
                            adatok[i].tavozasinapsorszam = Convert.ToInt32(record[4]);
                            adatok[i].vendegszam = Convert.ToInt32(record[5]);
                            adatok[i].kernekreggelit = Convert.ToInt32(record[6]);
                            adatok[i].vendegazonosito = record[7];
                            utolso++;
                            Console.WriteLine(i+" "+ record[7]);
                            i++;
                        }
                    }
                    sr.Close();
                    
                    for (i2 = 1; i2 <= utolso; i2++){
                        Console.WriteLine(
                            " Foglaloszam  "+ adatok[i].foglaloszam+
                            " Szobaszam    "+ adatok[i].szobaszam+
                            " Erkezesinap\n sorszama "+ adatok[i].erkezesinapsorszama);
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine(" Test "+utolso+ adatok[i].vendegazonosito);
        }
    }
}
 

Attachments

  • pitypang.txt
    31.8 KB · Views: 22
Just scanning through your code, it looks like i will always be zero and so you'll never ever update your utolso past zero.
 
You have i++ in else block (not i==0), so it won't update.
Split record is index [0-6], you try to read [1-7].
 
You have i++ in else block (not i==0), so it won't update.
Split record is index [0-6], you try to read [1-7].
thank you for help. i==0 is a comparison, I don't understand it, and how to increment? I understand split record but my very first value is 984, a single data so help needed in this matter too, thank you.
 
This is your code:
C#:
if (i == 0) 
{  
   kod0 = Convert.ToInt32(record[0]); 
}
else 
{ 
   i++; 
}
 
This is your code:
C#:
if (i == 0)
{ 
   kod0 = Convert.ToInt32(record[0]);
}
else
{
   i++;
}
thank you for help. I managed to make running the first part: read data from txt file in record list but the second part the for loop for work and display don't, giving error msg. can you help me please ? something is wrong with the indexing. thank you, frank.
hotelorder1:
 while (!sr.EndOfStream)
                    {

                        string sor = sr.ReadLine();
                        string[] record = sor.Split(' ');

                        adatok.Add(new pitypang00());
                        if (i == 0) { kod0 = Convert.ToInt32(record[0]); }
                        else
                        {
                            adatok[i].foglaloszam = Convert.ToInt32(record[0]);
                            adatok[i].szobaszam = Convert.ToInt32(record[1]);
                            adatok[i].erkezesinapsorszama = Convert.ToInt32(record[2]);
                            adatok[i].tavozasinapsorszam = Convert.ToInt32(record[3]);
                            adatok[i].vendegszam = Convert.ToInt32(record[4]);
                            adatok[i].kernekreggelit = Convert.ToInt32(record[5]);
                            adatok[i].vendegazonosito = record[6];
                            //i=0;
                            Console.WriteLine(" i " + i +" " + adatok[i].foglaloszam + " " + record[6] + " " + adatok[i].szobaszam + " " + adatok[i].erkezesinapsorszama);

                        }
                        i++;
                        if (i == 1) { Console.WriteLine(" i " + i + " " + kod0); }
                        
                    }
                    utolso=i;
                    Console.WriteLine(" utolso " + utolso);
                    sr.Close();
                    
                        for (i2 = 0; i2 < utolso; i2++)
                        {
                        if (i == 0)
                        {
                            Console.WriteLine(" Vendeg szam  " + kod0);
                        }
                        else
                        {
                            Console.WriteLine(
                                " Foglaloszam  " + adatok[i].foglaloszam +
                                " Szobaszam    " + adatok[i].szobaszam +
                                " Erkezesinap\n sorszama " + adatok[i].erkezesinapsorszama);
                        }
                    }
 
Your second loop index is i2.
 
Back
Top Bottom