CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2009
    Posts
    18

    CSV file in VC++

    Hi,

    Newbie on VC++. Do pls help. I would like to open a Comma Sep Value(CSV) file in VC++. The CSV is basically created in excel. Now I would like to make the open/close/read/write/modify on the data in the csv file through VC++ program, but I am at a loss to get it done. The user supplies the data through the dialogs created in VC++. Do help or give me pointers to it.

    Searched on this forum, but couldnt get any related to it.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: CSV file in VC++

    Since you want to modify the data and since you cannot just add data to the middle of a file, you will have to read the file into some kind of memory date structure. Then make all your changes in memory and at the end write your data back to the file.

    With which part do you have problems?
    Can you open a file and read the contents?
    Are you using MFC classes or std classes?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: CSV file in VC++

    Hi,

    What exactly do you want to do?

    open/close/read/write/modify ?

    You have to decide which way you want to go:
    • Automate Excel could be a good alternative to read/write/modify a cvs file. Search here or in the web for examples.
    • Do it the harder way. Then you've to open the file, parse it, save the data in a suitable data struct, invent a GUI for the user, and re-save the file.


    No one here will make the whole work for you.

    If you still think you've to make it the harder way please ask questions that can be answered here. For instance: How can I open a csv file? And please show us what you've done so far. Programming is not "asking somewhere and modify the code", is it?

    And btw: If you do not know how to open a (csv) file and how to step through the data in it (e.g. to parse it): Maybe you first have to learn c++? Then buy a good contemporary book and read it carefully.

    With regards
    Programartist

  4. #4
    Join Date
    Feb 2009
    Posts
    18

    Re: CSV file in VC++

    Hi Marc & Programartist,

    thank you for the response.

    What I can do now:
    - Open the file normally using open/read/write/close sys calls - both c and c++.
    Its the harder way and I don't want to use it.

    My questions:
    1. Can we use MFC to manipulate the excel file data.
    2. Is ODBC necessary for accessing the excel file?

    The problem:
    1. Being a newbie to windows & database stuff. I have tons
    of confusion between the different terminologies - mfc/win32/ole/dao/odbc....
    Hence the vague question.

    I am now attempting using the MFC COleVariant class to automate excel file data through vc++/mfc program way.

    Btw: I don't want code. Examples & pointers wud be enough. will post the code shortly as I am sure to run into problems

    Your help highly appreciated & Thank you for your time.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CSV file in VC++

    Search Google for msdn excel automation c++ example
    or MSDN for excel automation c++ example
    Victor Nijegorodov

  6. #6
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: CSV file in VC++

    Keep in mind that a CSV file is NOT an Excel file - it's a simple text file that can be opened with Excel, and Excel will attempt to make sense of the data.

    Since a CSV file is a text file, you could use the MFC class CStdioFile to open/read/write the file.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: CSV file in VC++

    Personally I think using Excel automation for manipulating a CSV file is a bit overkill.
    However, I don't know what kind of manipulations you are thinking about.
    Still, I would create a memory structure/class to hold the data for 1 row.
    Then use MFC CStdioFile to read the file and parse it into my datastructure. Put all rows in a list or vector and then you can easily manipulate it how ever you want. When finished, you iterate over your vector and write out the data from the structure back in the file in CSV format.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: CSV file in VC++

    Quote Originally Posted by Marc G View Post
    Personally I think using Excel automation for manipulating a CSV file is a bit overkill.
    Of course it is.

    But often users want to manipulate csv files since this is a text file excel can export. If the data someone want to edit is in an excel document it can be a good alternative to manipulate this data with excel (or with excel automation from VBA or from your own application) instead of exporting the data into a csv file, manipulate this and re-import it to excel.

    Question to the Original-Poster (janatech):
    - Do you have a cvs file or do you have data in excel?

    If your data is alredy in an excel document and you need the resulting data in an excel document too I recommend to see the links in VictorN's post. I tried this and had have success in a short time.

    If you really want to manipulate a csv file without using excel at all you should follow MarcG's steps.

    Quote Originally Posted by janatech
    1. Being a newbie to windows & database stuff. I have tons
    of confusion between the different terminologies - mfc/win32/ole/dao/odbc....
    Hence the vague question.
    win32: The Application Programmers Interface of the windows operating system.
    Needed here (directly or indirectly)

    ole: Object Linking and Embedding.
    A programming interface created by Microsoft to give applications the possibility to interact with other applications.
    Usable here but not necessary.

    dao Data Access Objects
    A generalized interface for databases from Microsoft.
    Not needed here at all (although it is possible to use it to interact with excel, but this will be a real overkill).

    odbc Open Database Connectivity
    A standard interface for accesing several databases.
    Not needed here (using it to manipulate excel or csv data will be a real overkill too).

    MFC Microsoft Foundation Classes
    An object oriented library from Microsoft wrapping the win32 API to easy-to-use objects.

    In the links given by VictorN you'll find all the necessary stuff you need to know to automate excel. There are nice wrapper classes for MFC and automating excel (search code guru for it).

    And an additional note: If you don't want to rely on an installed excel you have to go the harder way: Use plain win32 or MFC functions to read/parse the file.

    With regards
    Programartist

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CSV file in VC++

    Programartist is right.
    I'd, however, say that using ODBC or DAO with ISAM drivers to read/write from/to the text files (comma or something else, like semicolon, separated) is not a problem.... But in most cases it would be overkill as well.
    Victor Nijegorodov

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: CSV file in VC++

    @janatech

    This is kinda typical situation for being faced with a bunch of new technologies.

    Some of them could be seemingly easy (automating Excel for example), but would be really/hardly controllable for beginner with no experience in COM programming. Poorly documented, Excel automation in C++ easily could make you give up, as you feel like drowning in a swamp...

    Other of them could be controlled more easy way as long as this is all about CS basics - just reading/writing files, parsing techniques, standard library features like containers and algorythms. Lots of books/sites. But it finally requires for a discipline, you know. Never rush the things, make it sequential, reasonable, understand where you are now and where you're heading to...

    It's always hard to choose right way.
    Best regards,
    Igor

  11. #11
    Join Date
    Feb 2009
    Posts
    18

    Re: CSV file in VC++

    Thanks a lot guys. I decided not to use the odbc seemed like an overkill to learn too. I managed to do do it using the regular sys calls.

    Sorry, didnt respond imm as I was catching up with the schedule.

    Thanks a ton once again to all of u - Marc, P.Artist, Victor, Krmed, Igor.

Tags for this Thread

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