CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2002
    Posts
    936

    Export Data to CSV

    I am trying to write some data to a CSV format, but I do have a problem. Sime I have a lot of data rows, it looks like some data may not fall into the right column. To get started, I used the stream class to create the file like

    PHP Code:
    ofstream myFile("data.csv");
    //create the columns
    myFile<<"column1,"<<"column2,"<<"column3,"<<"column4,"<<endl
    I do have more colmns then that, but that is not an issue. It looks like I have to put the colmns in the std vector format, then read the colmns and append specific data to the right column and the right row. It is not straight forward, since I have different sections in the program where I have to read data from. I mean not a single function, but different functions.

    At the end, I would like to have something like that in the file

    column 1 column 2 colunn 3
    ====== ====== ======
    data 1 data 2 data 3
    data 4 data 5 data 6

    I am not familiar with std vector, how can I use std vector to store the columns and happend specific data to the right column and write it to the file? This is basically what I need

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Export Data to CSV

    Since CSV is capable of storing any type of data in each field, and std::vector is not, I don't think that's the tool you're looking for.

  3. #3
    Join Date
    Jun 2002
    Posts
    936

    Re: Export Data to CSV

    In term of data type, there should not be any problem. All my data are in "int",

    using

    <<intVariable

    Simply write them to a text file

    I did not think about the column headers, but they are not a part of the data. Since I know the number of column header, assume that I have 5 columns, can I stor them in a in a 2Vector in this case I have coumn1, 2, 3, 4, 5; then my data will look like

    1 2 3 4 5
    data1 data2 data3 data4 data5

    If I do it like that, can I seek for specific column and put the data to sepecific row?
    Last edited by vcstarter; September 24th, 2008 at 01:33 PM.

  4. #4
    Join Date
    Jun 2002
    Posts
    936

    Re: Export Data to CSV

    I used 2D array in order to solve the problem, but it looks like some data are written in the wrong column. I pass the data to couple of 2D array, then do something like that

    PHP Code:

    ofOutputFile
    <<data[i][j
    There are two problems with that, since the datat will be keep writing to the file, I simply use a constant length for testing only. Second, I have not been able to prevent some of the data written to the wrong column.

    Since the data are written from different thread (different function), I think I have also a problme to figure out when to put a carriage return like

    PHP Code:

    outputFile
    <<endl
    ;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured