Click here to Skip to main content
15,885,767 members

Record Removing Problem in C# File Handling

Member 11004573 asked:

Open original thread
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!

C#
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();
        }
    }
}
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900