Solved

getting data out of a CSV file.

Posted on 2000-03-05
3
238 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

929 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

13 Experts available now in Live!

Get 1:1 Help Now