Solved

getting data out of a CSV file.

Posted on 2000-03-05
3
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

615 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