File Append Problem

M

Matthew Lagace

Hello,

I am reading a csv file (File1) and putting its content into a string,
then I append that string in another csv file (file2) that has some data
in it already. Now I do not want to overwrite the data in file2 so i've
opened file2 in append mode, and the string from file1 is appended to
file2, but, I lose some characters in the first line of my string, it
looks almost like the first line of my string from file1 is trying to
overwrite the last line in file2 but it doesn't. My string just loses
the same amount of characters of the number of characters from my last
line that was already in file2. Long story short, It won't append
correctly.

Here is the code:

File.new(rightfile,"w")

fileNew = File.open(rightfile,"w")
fileNew.puts
'"email","source","ip","data_timestamp_proper","firstname","lastname"'

str = IO.read(outfile)

fileAppend = File.open(rightfile,"a")
fileAppend.puts str
 
M

Mariusz Pękala

--IpbVkmxF4tDyP/Kb
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hello,
=20
I am reading a csv file (File1) and putting its content into a string,
then I append that string in another csv file (file2) that has some data
in it already. Now I do not want to overwrite the data in file2 so i've
opened file2 in append mode, and the string from file1 is appended to
file2, but, I lose some characters in the first line of my string, it
looks almost like the first line of my string from file1 is trying to
overwrite the last line in file2 but it doesn't. My string just loses
the same amount of characters of the number of characters from my last
line that was already in file2. Long story short, It won't append
correctly.
=20
Here is the code:
=20
File.new(rightfile,"w")
=20
fileNew =3D File.open(rightfile,"w")
fileNew.puts
'"email","source","ip","data_timestamp_proper","firstname","lastname"'
=20
str =3D IO.read(outfile)
=20
fileAppend =3D File.open(rightfile,"a")
fileAppend.puts str
=20

Have you debugged what is the contents of str ?
Maybe the problem is not in appending but reading?

If this is the exact code you run, then you are opening the file three
times, without closing it or flushing its buffers:

First: File.new(rightfile,'w')
Second: File.open(rightfile,'w')
Third: File.open(rightfile,'a')

I suggest something like:

File.open( rightfile, 'w') do |f|
f.puts '"email","source"....'
end

str =3D IO.read(outfile)

File.open( rightfile, 'a') do |f|
f.puts str
end

but it may be shortened to just:

File.open( rightfile, 'w' ) do |right|
right.puts '"email","..."'
str =3D IO.read( outfile )
right.puts str
end

--=20
No virus found in this outgoing message.
Checked by 'grep -i virus $MESSAGE'
Trust me.

--IpbVkmxF4tDyP/Kb
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7-ecc0.1.6 (GNU/Linux)

iD8DBQFGoRrjsnU0scoWZKARAhHfAKC+vQrizOAqktPsSv9aS5xuHiJ45ACfdzgn
AAvotTWoENnGoaJOdMgFWtI=
=Jx5X
-----END PGP SIGNATURE-----

--IpbVkmxF4tDyP/Kb--
 
M

Matthew Lagace

File.open( rightfile, 'w' ) do |right|
right.puts '"email","..."'
str = IO.read( outfile )
right.puts str
end


Oh i see now, I have got to stop programming so structured. Thanks alot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top