• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
ozoCommented:
scanf("%[^,],%[^,],%[^,]",&field1,&field2,&field3); /* assuming nonempty fields */
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

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
rgoughCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.