LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-18-2013, 05:10 PM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Language C: Extracting fields into a CSV file


Hello,

A CSV file is a plain text file originally from Excel, notepad, that has some records.

The fields are separated by ";".

With a conventional C language (for gcc compiler), how would you separate the fields while reading a given data.csv file?

I am looking forward to hearing you.

Many thanks
 
Old 01-18-2013, 05:47 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
1. Open the CSV file (and verify such)

2. Use fgets() to read each line (one by one)

3. For each line read, use strtok() to separate the fields.

If you desire additional help, please post some code demonstrating that you have, at a minimum, followed my advice above.
 
1 members found this post helpful.
Old 01-21-2013, 06:42 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by Xeratul View Post
Hello,

A CSV file is a plain text file originally from Excel, notepad, that has some records.

The fields are separated by ";".
Technically, CSV files are Comma Separated Value files, so they use commas as delimiters. A robust CSV parser may use strtok() internally, but is likely to include a great deal more complexity than that. Dealing with embedded delimiters basically means inspecting, character by character, each byte in the file. Some 'CSV' files are not well-formed. How would you intend to deal with those?
I suggest first looking for a library that provides a working CSV parser API.

--- rod.
 
Old 01-22-2013, 10:13 AM   #4
evgenyz
Member
 
Registered: Sep 2012
Posts: 48

Rep: Reputation: Disabled
I wrote some CSV parser in C long time ago..
If I will find some souce files I will send them to you.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
[SOLVED] Extracting fields from an inconsistent database sharky Programming 4 08-15-2011 04:02 PM
How to sort a csv on three fields ? (cygwin user) turquoise_man Linux - Newbie 2 08-24-2009 09:12 AM
Parsing a comma separated CSV file where fields have commas in to trickyflash Linux - General 7 03-26-2009 03:30 PM
help extracting data from csv file willinusf Linux - General 10 10-27-2006 09:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration