Delete one line of file

I’m doing one work school, sorry don’t write english very good, I’m
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I’m using
delete_if, look at the code, I’ll attach it.

On Wed, Mar 9, 2011 at 9:55 PM, Joyce L. [email protected] wrote:

I’m doing one work school, sorry don’t write english very good, I’m
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I’m using
delete_if, look at the code, I’ll attach it.

(If I understand, we are looking at opcao == 2 (Excluir Aluno)?)

Here is a small example of one way to copy from one array to another:

input = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
output = []

input.each do |n|
if n == 4
# skip it
else
output << n
end
end

p output #=> [1, 2, 3, 5, 6, 7, 8, 9, 10]

Working with files, I might do something like:

  • Create a temporary output file
  • Write to the output file while reading from the original input
  • When finished, move temporary file over the original

is there any off campus for mca 2011 pass out in india means , plz
inform
thank u

regards
j.raja

Hi Joyce,

First you should read the file:

alunos = IO.readlines(‘alunos.txt’) # return an array with all ids

Remove the selected id:

alunos.delete_if do |linha|
dados = linha.split(":") #“quebra” quando encontrar dois pontos
outrosDados = dados[0].split("!")

outrosDados == login

end

Open the file and rewrite the content

f = File.open(‘alunos.txt’, ‘w’)
alunos.each |a|
f.puts a
end
f.close

This should work!

I myself use File.readlines but it seems IO.readlines is doing the same

  • reading into an array.

When you have an array with the data you can just omit part of that
array and then store that result.

Perhaps there is a more elegant approach to it but it is what I usually
do when I store a modified file.

Thiago Lewin wrote in post #986702:

Hi Joyce,

First you should read the file:

alunos = IO.readlines(‘alunos.txt’) # return an array with all ids

Remove the selected id:

alunos.delete_if do |linha|
dados = linha.split(":") #“quebra” quando encontrar dois pontos
outrosDados = dados[0].split("!")

outrosDados == login

end

Open the file and rewrite the content

This should work!

…unless your computer crashes right after you open the file. You
have to use a temporary file as unkown posted. The Tempfile module is
helpful in that regard.

Thiago Lewin wrote in post #986702:

Hi Joyce,

First you should read the file:

alunos = IO.readlines(‘alunos.txt’) # return an array with all ids

Remove the selected id:

alunos.delete_if do |linha|
dados = linha.split(":") #“quebra” quando encontrar dois pontos
outrosDados = dados[0].split("!")

outrosDados == login

end

Open the file and rewrite the content

This should work!

…unless your computer crashes right after you open the file. To
guard against losing all your data, you have to use a temporary file as
unkown posted. The Tempfile module is helpful in that regard.

Thiago Lewin wrote in post #986702:

Hi Joyce,

First you should read the file:

alunos = IO.readlines(‘alunos.txt’) # return an array with all ids

Remove the selected id:

alunos.delete_if do |linha|
dados = linha.split(":") #“quebra” quando encontrar dois pontos
outrosDados = dados[0].split("!")

outrosDados == login

end

Open the file and rewrite the content

This should work!

…unless your computer crashes right after you open the file. To
guard against losing all your data, you have to use a temporary file–as
unkown posted. The Tempfile module is helpful in that regard.

The basic rule is: you can’t alter files; you can only read them and
output altered lines to another file, then delete the original file, and
rename the new file to the original file name.

Thank you all, I understood the idea of a temporary file and I think
this will solve my problem.