Solved

getting data out of a CSV file.

Posted on 2000-03-05
3
236 Views
Last Modified: 2010-04-02
I need to pull data from a .CSV file.
I have opened the file using fopen(stream,"r").  How do I get the data that is there? There are 9 collums and unlimitted number of rows with data in them. Once I have the data I need to pass it to multiple CString varibles for viewing. Examples of code would be much appreciated.
PS I can not use ODBC or DAO to accomplish this task.  I am using VC++ 6.0.
0
Comment
Question by:Argonauts
  • 2
3 Comments
 

Accepted Solution

by:
jclayton earned 200 total points
ID: 2586489
OK then, you've opened the file using the old IO library (I presume you don't want to use the new one?).

Here's some simple code that could easily be shunted into a CLASS that handles reading CSV files.  Made a quick look on the msdn.microsoft.com site too - but not much there (only an ODBC solution).

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifndef WIN32
#include <unistd.h>
#include <sys/types.h>
#endif

#define BUFFSIZE 1024

int main(int argc, char *argv[])
{
    // open a file for reading
    FILE *filep = fopen("file.csv", "r");

    if(filep == NULL)
    {
        return 1; // boom
    }

    // load each line into a buffer
    char *buffer = new char[BUFFSIZE]; // assume we've not got lines longer than this?

    if(buffer == NULL)
    {
        delete [] buffer;
        return 2; // oops, no mem?
    }

    int bytesRead = fread(buffer, 1, BUFFSIZE, filep);
    while(bytesRead != 0)
    {
        // process this buffer line into tokens, you should not that this routine will NOT handle
        // embedded quotes within a CSV item - you should write this properly or get it off the net.
        char *tokdelim = ",\t\r\n";
        char *tokptr = strtok(buffer, tokdelim);
        while(tokptr)
        {
            // do something with the token
            printf("'%s'\r\n", tokptr);
            tokptr = strtok(NULL, tokdelim);
        }

        bytesRead = fread(buffer, 1, BUFFSIZE, filep);
    }

      fclose(filep);

    delete [] buffer;
    return 0; // no errors - ok !
}
0
 

Expert Comment

by:jclayton
ID: 2586500
Oops, forgot the next obvious bit.  Presumably you can keep a count (that is reset on each line read) of the column you've just read, and then simply use a case statement to push the data onto the CString values?

Johno.
0
 
LVL 1

Author Comment

by:Argonauts
ID: 2590321
That looks to be right.  I appreciate it.  I will give it a try and see what happens.  You also mentioned something about a new style.  If you could just give me a keyword on that I will look it up.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now