Search and print console data from a .txt in C #

1

Hello :) Well it is a simple program in which a .txt is handled, the program has the option to enter data, print them and search ....

My problem is in the third part: Search, the data that is saved in the txt are: Password, Name and Position as follows:

  

123
Carlos Dayan Rodriguez Perez
Administrator
  -Espacio-
234
Diego Antonio Rodriguez Perez
Programmer
  -Spacio
456
Juan Camaney
Tester
  -Spacio

Notice what it says -Spa- with that indicated that instead of a line break emptied every one that ends a "record" there is a space, it is because of this ......

The code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Programa1_Agenda
{
    class Program
    {
        static StreamReader Leer;
        static StreamWriter Escribir;

        static void Main(string[] args)
        {
            int Op;
            int op_1 = 0;
            do
            {
                Console.Clear();
                Console.WriteLine("AGENDA 2018\n\n");
                Console.WriteLine("1) Ingresar datos\n2) Mostrar datos\n3) Buscar persona\n4) Salir\n\nTu opcion: ");
                Op = int.Parse(Console.ReadLine());

                if (Op == 1)
                {
                    do
                    {
                        int DUI;
                        string nombre;
                        string puesto;
                        Console.Clear();
                        Console.WriteLine("Escribir un nuevo registro\n\n");
                        Escribir = new StreamWriter("Archivo_Agenda.txt", true);
                        //INGRESA DATOS
                        Console.Write("\n* Ingresar nueva clave DUI (3 Digitos enteros): ");
                        DUI = int.Parse(Console.ReadLine());
                        Escribir.WriteLine(DUI);
                        Console.Write("\n* Ingresar nombre completo: ");
                        nombre = Console.ReadLine();
                        Escribir.WriteLine(nombre);
                        Console.Write("\n* Ingresar puesto: ");
                        puesto = Console.ReadLine();
                        Escribir.WriteLine(puesto);
                        Console.WriteLine("\n\n");
                        String Cadena = Console.ReadLine();
                        Escribir.WriteLine(Cadena);
                        Escribir.Close();

                        Console.WriteLine("El registro se ha creado exitosamente.\n\n5) Regresar al menu principal\n6) Ingresar un nuevo usuario");
                        Console.Write("\nTu opcion: ");
                        op_1 = int.Parse(Console.ReadLine());
                    } while (op_1 == 6);
                }

                if (Op == 2)
                {
                    string Linea;
                    int contador = 0;
                    Console.Clear();
                    Leer = new StreamReader("Archivo_Agenda.txt", true);
                    Console.WriteLine("Mostrando todos los registros:\n\n");
                    while((Linea = Leer.ReadLine()) != null)
                    {
                        Console.WriteLine(Linea);
                        contador++;
                    }
                    Leer.Close();

                    Console.WriteLine("\nEl registro se mostro exitosamente.\n\n5) Regresar al menu principal\n");
                    Console.Write("\nTu opcion: ");
                    op_1 = int.Parse(Console.ReadLine());
                }

                if (Op == 3)
                {
                    string Linea;
                    int contador = 0;
                    string result_s;
                    Console.Clear();
                    Leer = new StreamReader("Archivo_Agenda.txt", true);
                    Console.WriteLine("Buscar registro por medio de clave DUI:\n\n");
                    Console.Write("Ingresa la clave DUI: ");
                    result_s = Console.ReadLine();
                    if((Linea = Leer.ReadLine()) == result_s)
                    {
                        do
                        {
                            Console.WriteLine(Linea);
                            contador++;
                        } while ((Linea = Leer.ReadLine()) != " ");
                    }
                    Leer.Close();

                    Console.WriteLine("\nEl registro se mostro exitosamente.\n\n5) Regresar al menu principal\n");
                    Console.Write("\nTu opcion: ");
                    op_1 = int.Parse(Console.ReadLine());
                }
                Console.ReadKey();
            } while (op_1 == 5);
        }
    }
}

What I try to do in option 3, is that by asking for the DUI key and being detected in the file, the following data will be printed on the screen until a space is found (and this is the reason why ...)

This is done if the first record is entered: 123, and prints the correct thing:

123
Carlos Dayan Rodriguez Perez
Administrator

But if I enter any of the other 2, nothing is printed ...: (

Please help, I do not know what to do xd

    
asked by CarlosDayan_ 04.10.2018 в 07:39
source

1 answer

1

Very good Carlos, As Raul says, the problem is that you must use a first loop to find the key (if it exists), and once found, you can use the loop you have to print the information by console.

Here is an example of the Option 3 running using two loops do-while :

   if (Op == 3)
    {
        string Linea;
        int contador = 0;
        string result_s;
        Console.Clear();
        Leer = new StreamReader("Archivo_Agenda.txt", true);
        Console.WriteLine("Buscar registro por medio de clave DUI:\n\n");
        Console.Write("Ingresa la clave DUI: ");
        result_s = Console.ReadLine();
        do
        {
            Linea = Leer.ReadLine();
        } while (Linea != result_s && Linea!= null);

        if ((Linea == result_s))
        {
            do
            {
                Console.WriteLine(Linea);
                Linea = Leer.ReadLine();

            } while (Linea !=" ");
        }
        Leer.Close();

        Console.WriteLine("\nEl registro se mostro exitosamente.\n\n5) Regresar al menu principal\n");
        Console.Write("\nTu opcion: ");
        op_1 = int.Parse(Console.ReadLine());
    }
    
answered by 04.10.2018 / 09:01
source