reading a comma delimited file

Is there an easy way to read and parse a comma delimited, non-fixed width file, without reading and analyzing one character at a time.
aricheeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rgoughConnect With a Mentor Commented:
Use strtok.

example

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

using namespace std;

void main()
{
      char str[] = "This,is,a,test,string";
      char *s;

      s = strtok(str, ",");
      while (s)
      {
            cout << s << endl;
            s = strtok(NULL, ",");
      }
}
0
 
idtCommented:
In order to determine field deliniation, you will need to 'look' at each char, but

In order to prevent your app from becoming a slug, I suggest that you allocate a buffer of some size and read into that buffer, then perform your reading of each char,

repeating the reads into your buffer until you read in less than bytes than your buffer len.


-iDT

0
 
JHuangCommented:
You may use:

istream & getline(signed char * buff, int size, char delimiter='\n');

#include "fstream.h"

#define MAX_LENGTH 100
char    buff[MAX_LENGTH];
istream fs;

// Open File//

while (!fs.eof())
{
    fs.getline(buff, MAX_LENGTH-1, ',');

//process fs//

}
0
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

 
ozoCommented:
scanf("%[^,],%[^,],%[^,]",&field1,&field2,&field3); /* assuming nonempty fields */
0
 
stochasticCommented:
arichee,

If this is a one-time requirement, you could get by without writing a program, by using Excel to do this for you. It is really quite simple in Excel. Once you get it parsed into separate columns in Excel, you can save it back in a variety of formats, including plain text. Tell me if this will suit your purpose, in which case I will explain in detail.
0
 
aricheeAuthor Commented:
Unfortunately, this has to be an automated process.
0
 
aricheeAuthor Commented:
I was afraid of that, but I guess it's the best way.
0
 
KangaRooCommented:
You can not (easily) avoid reading char by char AND demand no upper limit for the line length.
0
 
aricheeAuthor Commented:
I tried that, but when I compile it, I get the below error:

c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error :  "eh.h is only for C++!"
Error executing cl.exe.
0
 
rbrCommented:
So why did you accept the answer.
To all: This is the C section not the C++ section. So pls post C code.
0
 
rgoughCommented:
OOPS!
I had answered a question in the C++ section prior to this one.
See revised C example below.

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

main()
{
   char str[] = "This,is,a,test,string";
   char *s;

   s = strtok(str, ",");
   while (s)
   {
      printf("%s\n", s);
      s = strtok(NULL, ",");
   }
}
0
 
aricheeAuthor Commented:
Thanks, it works great!
0
All Courses

From novice to tech pro — start learning today.