Navigating Columns and Rows of a CSV file

This is a discussion on Navigating Columns and Rows of a CSV file within the C++ Programming forums, part of the General Programming Boards category; Hi, The Problem: Navigating through a CSV file to write data in specific columns and rows. This is the logic ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool Navigating Columns and Rows of a CSV file

    Hi,

    The Problem: Navigating through a CSV file to write data in specific columns and rows.

    This is the logic behind the code I am planning:
    - Create the CSV file.
    - Enter loop which will run the core function numerous times produces numerous sets of data to be saved to the csv file.
    - Close and clear.

    The problem I foresee is this...
    Each run of the core function will produce 2 columns of data. Each time I run the core function I would like to append further columns to the csv file. e.g.) After run 1 columns A and B are filled with data, after run 2 - further data is added to columns C and D, etc.

    My question is this, how can I write, for example, to the 1st row of the 3rd column after my initial data write??? To put it another way, after the first set of data has been written to columns A and B, how do I code for then writing to the next available column space for the next set of data?

    Thanks for reading. Any suggestions will be gratefully received.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A nice and easy way, if the nature of the data permits it (columns can have a fixed width), and if there is a fixed and consistent number of columns, would be to use seekp/seekg.

    Otherwise you will have to work line by line using the comma as an input separator. If your columns per row is consistent, you could also just ignore line breaks and count items.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or, if you want to make sure you have a CSV file after every run (not clear to me whether this is what you want or not), then you write the original CSV file the first time your core function runs, then every time after that you open the previous CSV, read a line, add to it, and write it out (somewhere else).

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks for your suggestions.

    I will look further in to seekp/seekg.

    I only want 1 CSV file. Ideally, it will be created and at the start of the program, be added to as the function is called and then it will be closed before the program ends.

    The thing is, on each core function call there will be an additional data set produced which I want to add to the CSV file. Each data set comprises of 2 columns of data (the number of rows may vary between data sets).
    Please see attached JPEG for clearer understanding of what I am hoping to do.
    Basically, I am wanting to append new columns of data each run.


    Thanks again
    Attached Images Attached Images  

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Alright, it says C++ at the top of the page: Why not build a vector of vectors, and then at the very end write it out to a file? If you have rows where you only have elements in columns 5, 6, 9, and 10, you'll have some work to do, but otherwise it looks like what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read columns and rows
    By university in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:09 AM
  2. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  3. printing rows and columns
    By datainjector in forum C# Programming
    Replies: 1
    Last Post: 08-03-2003, 05:42 PM
  4. calculation from rows and columns
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-08-2002, 07:44 PM
  5. Adding Columns and Rows..Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21