4 Replies - 2376 Views - Last Post: 12 May 2009 - 07:56 AM Rate Topic: -----

#1 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

How to write a string to a determined line in TextFile?

Post icon  Posted 12 May 2009 - 06:46 AM

Hey bros. Finally got the other problem working, turns out I was doing it wrong. Nevermind.

Anyways, I finally figured out how to search for a determined record in my List<PeopleClass> List:

public static void ModificarDatos()
        {
            List<string> MyList = new List<string>(Persona.LeerArchivo());
            Persona ObjectPersona = new Persona();
            LeerDatos();            

            Console.WriteLine("Para modificar el dato de una persona escriba su carnet de identidad, y se jalara su record de datos.");
            string SelectedCarnet = Console.ReadLine();

            foreach (Persona Q in ObjectPersona.obtenerPersonas())
            {
                if (Q.Carnet == SelectedCarnet)
                {
                    Console.WriteLine("Entrada encontrada exitosamente.");
                    Console.WriteLine("Datos: " + Q.Nombre + "||" + Q.Apellido + "||" + Q.Carnet + "||" + Q.DOB + "||" + Q.Sexo);
                    Console.WriteLine("Escriba los datos que desea modificar utilizando el siguiente formato:");
                    Console.WriteLine("nombre;apellido;carnet;fecha de nacimiento;sexo");
                    string EditedInfo = Console.ReadLine();

                    string[] DataArray = EditedInfo.Split(';');
                    Q.Nombre = DataArray[0];
                    Q.Apellido = DataArray[1];
                    Q.Carnet = DataArray[2];
                    Q.DOB = Convert.ToDateTime(DataArray[3]);
                    Q.Sexo = DataArray[4];

                    Q.guardarPersona();//<---This method grabs the modified data and saves it to another line in the TextFile. What I want to it to do is delete X number line and replace that line with the the contents of Q.              
                }               
            }               
        }


Two questions:

1. How can I write a line to a specific line number in the text file?

2. How can I erase the contents of a specific line number in a text file?


I think it's pretty easy for you l337 ninja coders. :ph34r:


I'll await in the shadows.

Is This A Good Question/Topic? 0
  • +

Replies To: How to write a string to a determined line in TextFile?

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to write a string to a determined line in TextFile?

Posted 12 May 2009 - 07:18 AM

there is no built-in function to write to a specific line of a text file. You have to read in the entire file, line by line, keep up with the line number, and insert/update/delete when you get to the line that you want to do something with.
Was This Post Helpful? 0
  • +
  • -

#3 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: How to write a string to a determined line in TextFile?

Posted 12 May 2009 - 07:21 AM

Ok, let's say I scroll the entire text file and I keep a small counter counting until I find the matching string, how can I:

delete that string.
replace that string with whatever string I have saved.

I understand the pseudo code, I just don't know what methods to call. Do you mind writing the code for me? It must be two or three lines. xD

Plus, I'll give ya a cold one:

Posted Image

This post has been edited by papuccino1: 12 May 2009 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to write a string to a determined line in TextFile?

Posted 12 May 2009 - 07:50 AM

it's actually very simply. You don't actually have to "replace", "delete", or "update" the line. Once you get to the line, you simply write the new line back to the file.

Contents of Text File Before..
this is
my testing
text file



Code:
static void Main(string[] args)
{
    int lineCounter = 1;
    StringBuilder sb = new StringBuilder();

    using (TextReader tr = new StreamReader(@"C:\Test\Test.txt"))
    {
        // using the Peek method will let you know if
        // there are more characters left in the file.
        // it will return a -1 when it's at the end of the file
        while (tr.Peek() > 0)
        {
            string line = tr.ReadLine();

            // this will replace the a line with another line
            if (line == "my testing")
                line = "your testing";

            // this will replace a specific line, regardless of text
            if (lineCounter == 3)
                line = "not my text file";

            // this will "delete" a specific line, regardless of text
            // you are not actually "deleting", you are just not writing it 
            // back to the file.
            if (lineCounter != 1)
                sb.AppendLine(line);

            lineCounter++;
        }
    }

    using (TextWriter tw = new StreamWriter(@"C:\Test\Test.txt"))
    {
        tw.Write(sb.ToString());
    }
}



Text file AFTER:
your testing
not my text file


Was This Post Helpful? 1
  • +
  • -

#5 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: How to write a string to a determined line in TextFile?

Posted 12 May 2009 - 07:56 AM

Massive thanks, I'll get crackin'. Get this thing to work!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1