tags:

views:

108

answers:

3

hi,guys.........

i am doing a VB program using four for loops to generate a set of numbers using the following code.....

 For sphere = 1 To 10
        For cylinder = 2 To 20
            For axis = 3 To 30
                For add = 4 To 40
                   Console.WriteLine("{0} , {1},{2},{3} ", _
                       sphere, cylinder, axis, add)
                Next add
            Next axis
        Next cylinder
    Next sphere

i need to export the output of this program to CSV format...... can any one help me by giving a suitable code for doing the same.......

+1  A: 

It looks like that code already generates CSV output. So one option if this is a console app, redirect the output to a file? From a command prompt...

myProgThatGeneratesNumbers.exe > outputfile.csv

Failing that if you do want to do it programatically , just open a stream and write out to that.

Chris J
+5  A: 

A CSV format is simply a text file that contains lines, with each line containing a record of data values separated by commas.

The easiest way to do this would be to invoke your application from the command line and redirect standard output to the command line using the > operator. That way, you don't need to change any code at all.

If, for some reason, you can't redirect standard output, Microsoft has an example on how to read from and write to a file using Visual Basic 2005.

I'm not intimately familiar with Visual Basic, but your code would probably look something like this:

Dim objStreamWriter As StreamWriter

objStreamWriter = New StreamWriter("output.txt", True, Encoding.Unicode)

For sphere = 1 To 10
    For cylinder = 2 To 20
        For axis = 3 To 30
            For add = 4 To 40
                objStreamWriter.WriteLine("{0},{1},{2},{3}", sphere, cylinder, axis, add)
                Next add
        Next axis
    Next cylinder
Next sphere

objStreamWriter.Close()

Note that the above code has not been compiled, much less tested. I just quickly made an example up to show you about what your code should look like to solve this problem.

Thomas Owens
Good answer, nice clean example.
David Stratton
I wrote one VB.NET app ever, and that was several years ago...not to mention, it's only 8am on a Saturday. If you find a problem with it, feel free to jump in and edit it to fix it up nice.
Thomas Owens
A: 

I would use a System.IO.StreamWriter to write the file.

David Stratton