# Problem writing a Faster\_csv generated record to a file

**URL:** <https://www.ruby-forum.com/t/problem-writing-a-faster-csv-generated-record-to-a-file/160067>\
**Category:** Ruby\
**Created:** [February 11, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/problem-writing-a-faster-csv-generated-record-to-a-file/160067 "2009-02-11T13:36:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@thiel](https://www.ruby-forum.com/u/thiel)\
**Post date:** [February 11, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/problem-writing-a-faster-csv-generated-record-to-a-file/160067/1 "2009-02-11T13:36:37Z")

</div>

Hi,

I have a problem writing a record generated with FasterCSV to a file:

This is the code:

op = File.new(“C:/Test/testmap.csv”, “w”)  
for i in 0 … n  
for j in 0 … 12  
custrecord[j] = x[i,j]  
end  
csv\_string = FasterCSV.generate\_line(custrecord)  
csv\_string.chop!  
csv\_string.gsub!(/,/, ‘;’)  
op.puts csv\_string  
end

The result in folder Test is: testmap.csv --\> 0Kb!! (Type CSV file)

It seems a very simple program which should work.

Can anyone tell me what’s wrong?

Thanks in advance,

Thiel

---

<div class="post-metadata">

**Author:** ![thiel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@thiel](https://www.ruby-forum.com/u/thiel)\
**Post date:** [February 11, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/problem-writing-a-faster-csv-generated-record-to-a-file/160067/2 "2009-02-11T15:36:19Z")

</div>

thiel wrote:

> This is the code:

Only part of it - you haven’t shown what sort of object custrecord is,  
for example.

> op = File.new(“C:/Test/testmap.csv”, “w”)  
> for i in 0 … n  
> for j in 0 … 12  
> custrecord[j] = x[i,j]  
> end

STDERR.puts “custrecord = #{custrecord.inspect}”

> ```
> csv_string = FasterCSV.generate_line(custrecord)
> csv_string.chop!
> csv_string.gsub!(/,/, ';')
> op.puts csv_string
> end
> 
> ```
> 
> The result in folder Test is: testmap.csv → 0Kb!! (Type CSV file)

That STDERR line will tell you whether custrecord contains what you  
expect. If not, work backwards until you find the problem (e.g. look at  
what’s in x)

There are simpler ways to drive fastercsv than your code though. Try  
this:

require ‘rubygems’  
require ‘fastercsv’  
FasterCSV.open(“/tmp/test.csv”, “w”, :col\_sep=\>“;”) do |csv|  
custrecord = [“foo”,“bar”,“baz”]  
csv \<\< custrecord  
end

The documentation for fastercsv is within the code, which may be  
installed somewhere like …/gems/fastercsv-1.4.0/lib/faster\_csv.rb on  
your system.

Or type “gem server --daemon” then point your web browser at  
[http://localhost:8808/](http://localhost:8808/)

---

<div class="post-metadata">

**Author:** ![thiel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@thiel](https://www.ruby-forum.com/u/thiel)\
**Post date:** [February 11, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/problem-writing-a-faster-csv-generated-record-to-a-file/160067/3 "2009-02-11T17:22:08Z")

</div>

Hi Brian,

Many thanks for your suggestion to use the inspect method. That revealed  
a ‘beginners’ program error. 😃  
After having made the correction I applied your solution, and it made  
the code simpler. And what’s more important: It works!

Here is the code snippet:

FasterCSV.open(“c:/Test/test.csv”, “w”, :col\_sep=\>";") do |csv|

```
  for i in 0 .. n
    for j in 0 .. 12
      klantrecord[j] = x[i,j]
    end
    csv << klantrecord
   end
end

```

Best regards,

Thiel

Brian C. schreef:
