Solved

getting data out of a CSV file.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 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 clear a vector as well as how to detect empty vectors in C++.

789 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