Solved

getting data out of a CSV file.

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Task manager indicates my c++ program memory consumption is growing? 12 140
Socket Programming (Unix) 8 153
Safe conversion? 4 76
Need some help with mailto 16 34
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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.

696 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