Click here to Skip to main content
14,330,512 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello Everyone!

Please See the Code and Tell me Solution,Problem When i Delete The Records From the File then it Only Delete "Student Name and Age "And Did not Delete The Student Roll Number i try my Best But I Did Not Understand The Problem Please Tell me If Any One Know About this.Please Help Me!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace File_Stream_1
{
    class Program
    {
        static void Main ( string[] args )
        {
            string Name;
            int Age, Roll;
            string FileName = "Text.txt";
                                 /*Data Writing Session in The File
                         ====================================================*/

            //TextWriter Writer = File.AppendText(FileName);
            TextWriter Write_Data = new StreamWriter(FileName,true);
            Console.Write("Enter Name:");
            Name = Console.ReadLine();
            Write_Data.WriteLine(Name);
            Console.Write("\nEnter Age:");
            Age = Convert.ToInt16(Console.ReadLine());
            Write_Data.WriteLine(Age);
            Console.Write("\nEnter Roll Number:");
            Roll = Convert.ToInt16(Console.ReadLine());
            Write_Data.WriteLine(Roll);
            Write_Data.Close();
                                 /*Data Reading Session in The File
                         ====================================================*/
            //StreamReader Read_Data = new StreamReader(FileName);
            TextReader Read_Data=new StreamReader(FileName);
            Console.Write("\nThe Enter Data Is.\n");
            string Line; int Count = 0;
            while (Read_Data.Peek() != -1)
            {
                Console.Write("\nStudent Name:");
                Line = Read_Data.ReadLine();
                Console.Write(Line);
                Console.Write("\nStudent Age:");
                Line = Read_Data.ReadLine();
                Console.Write(Line);
                Console.Write("\nStudent Roll Number:");
                Line = Read_Data.ReadLine();
                Console.Write(Line);
                ++Count;
            }
            Read_Data.Close();
                                  /*Data Removing Session in The File
                         ====================================================*/
            string Name_Remove, Copy_File = "Copy_Data.txt"; 
            Console.Write("\n\nEnter Name For Removing The Recods:");
            Name_Remove = Console.ReadLine();
            //Now Make Other File To Remove The Data
            TextReader Remove_Data = new StreamReader(FileName);
            TextWriter Copy_Data = new StreamWriter(Copy_File);
            while (Remove_Data.Peek() != -1)
            {
                if (Name_Remove != Remove_Data.ReadLine())
                {
                    //Take Name And Copy it Another File
                    Name_Remove = Remove_Data.ReadLine();
                    Copy_Data.Write(Name_Remove);
                    //Take Age And Copy it Another File
                    Name_Remove = Remove_Data.ReadLine();
                    Copy_Data.Write(Name_Remove);
                    //Take Roll Number And Copy it Another File
                    Name_Remove = Remove_Data.ReadLine();
                    Copy_Data.Write(Name_Remove);
                    Found = 1;
                    Console.Write("\nRecord Is Founded.");
                }
            }
            Copy_Data.Close();
            Remove_Data.Close();
            File.Delete(FileName);
            File.Move(Copy_File, FileName);
            if (Found == 0)
            {
                Console.Write("\nRecords Is Not Found.");
            }
            TextReader NewFile = new StreamReader(FileName);
            while (NewFile.Peek() != -1)
            {
                Console.Write("\nStudent Name:");
                Name = NewFile.ReadLine();
                Console.Write(Name);
                Console.Write("\nStudent Age:");
                Name = NewFile.ReadLine();
                Console.Write(Name);
                Console.Write("\nStudent Roll:");
                Name = NewFile.ReadLine();
                Console.Write(Name);
            }
            Console.ReadKey();
        }
    }
}
Posted
Updated 6-Dec-14 8:14am
v2
Comments
PIEBALDconsult 6-Dec-14 15:07pm
   
What file? Got an example?
ZurdoDev 6-Dec-14 15:28pm
   
If you debug the code and step through line by line you'll see exactly what is happening.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

First of all, use proper names. E.g. you use Name_Remove carelessly, no wonder you don't understand the code any more.
Second, you read the initial Name_Remove without storing it - so you loose the initial field entry and so get progressively out-of-sync with your data.
Cheers
Andi
   
v2
Comments
BillWoodruff 7-Dec-14 1:09am
   
+5 I think you found the real error source.

minor comment: the problem is that while the OP does store the Name_Remove value (read from the Console) once, they then re-set its value in each execution of the 'while loop.
Andreas Gieriet 7-Dec-14 1:19am
   
Thanks for your 5.
You are right: the value gets reset an the result of the ReadLine call in the "if" gets lost.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100