LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-27-2006, 11:24 PM   #1
ErrorBound
Member
 
Registered: Apr 2006
Posts: 280

Rep: Reputation: 31
A seemingly simple problem (C/C++)


Hi everyone,

I've written a program in C++ that takes a .csv (comma separated value) file as input and outputs a formatted table that LaTeX understands.

Anyway...it works as it is but requires the user to enter the number of columns in the .csv. The missing link is how to determine the number of columns in a table in this format. How could this be implemented? I imagine there must be something in one of the C or C++ standard libraries that could help me out?

All it needs to do is determine the number of columns in a file like this:
Code:
1,6,11
2,7,12
3,8,13
4,9,14
5,10,15
Here they are shown as integers but in general they could be floats or doubles.

Thanks for helping me out,
 
Old 05-28-2006, 01:13 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
What's wrong with a simple loop like this:
Code:
  int ncols = 0;
  ...
  << Open your CSV file >>
  while (fgets (buff, sizeof buff, fp) != NULL)
  {
    if (!ncols) 
    {
      for (int i=0; i < strlen (buff); i++)
      {
        if (buff[i] == ',')
          ncols++;  // Increment "ncols" for each comma
      }
      ncols++;  // #/columns = (#/commas + 1)
    }    
    ...
  }
Just a thought .. PSM

Last edited by paulsm4; 05-28-2006 at 01:15 AM.
 
Old 05-28-2006, 01:14 AM   #3
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
EDIT: paulsm4 posted a split second before me... same suggestion. sorry.

read the first line
count the number of commas
add one

Last edited by zhangmaike; 05-28-2006 at 01:17 AM.
 
Old 05-28-2006, 03:52 AM   #4
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Rep: Reputation: 34
Not direct advice, but I'd wonder if you could make a simple shell script that does exactly that instead of resorting to writing a C++ program?
 
Old 05-28-2006, 10:30 AM   #5
ErrorBound
Member
 
Registered: Apr 2006
Posts: 280

Original Poster
Rep: Reputation: 31
Count the commas! Why didn't I think of that!!

paulsm4 && zhangmaike: Thanks for the suggestion, it works beautifully.

daihard: I'm sure you could, but I don't have any experience writing shell scripts and so I'm just using what I know.
 
Old 05-28-2006, 12:36 PM   #6
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Rep: Reputation: 34
Quote:
Originally Posted by ErrorBound
daihard: I'm sure you could, but I don't have any experience writing shell scripts and so I'm just using what I know.
Fair enough. I was in the same camp until very recently - knowing only C/C++ and not much shell scripting, so I know exactly what you mean.
 
Old 05-28-2006, 01:44 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here's one way to do it in a shell script:
Code:
head -1 myfile.csv|awk -F, '{print NF}'
"head -1 myfile.csv" lists only the first line in your CSV file. We pipe that line to "awk".

"-F," tells awk to separate fields using a comma (",") instead of a space or a tab.

NF is a built-in variable containing the #/fields in this line (i.e. #/columns in your file).

The output from your example would be "3".

Here's a good tutorial on bash scripting (one of many!):
http://www.freeos.com/guides/lsst/

Unfortunately, shell scripting isn't something you can "just learn" (by reading a book or a tutorial), because it's less about learning the syntax of a specific language ("bash" or "ksh") than it is about learning how to use all the different tools in the environment (sed, awk, cut, paste, etc etc) effectively.

Fortunately, getting started is easy (the basics are really simple), and gaining proficiency is fun (the more you play with it, the more you want to learn more).

IMHO .. PSM

Last edited by paulsm4; 05-28-2006 at 01:46 PM.
 
  


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
ip of my server changes, seemingly at random esteeven Linux - Networking 7 03-10-2007 08:18 AM
Configurable HW seemingly dead? Recirqie Linux - Hardware 10 12-09-2004 05:16 PM
Computer crashing seemingly randomly raela Linux - General 10 05-08-2004 06:40 PM
The seemingly unsolvable problem of slow Samba PDC danywu Linux - Networking 1 09-21-2003 10:06 AM
Seemingly random hostname changes xanthumn Linux - Networking 2 01-21-2003 03:30 AM

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

All times are GMT -5. The time now is 08:50 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