LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-05-2008, 11:33 AM   #1
bb0330
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
C++ read & search in .csv files


for example if i have the following .csv file

ID Name CountryCode District Population
3793,NewYork,USA,NewYork,8008278
3794,LosAngeles,USA,California,3694820
3795,Chicago,USA,Illinois,2896016
3796,Houston,USA,Texas,1953631
3797,Philadelphia,USA,Pennsylvania,1517550
3798,Phoenix,USA ,Arizona,1321045
3799,SanDiego,USA,California,1223400
3800,Dallas,USA,Texas,1188580
3801,SanAntonio,USA,Texas,1144646

how can i cout " Texas" when i search houston?
how can i cout the lines from ID 3793 to 3799?
how can i compare the values of the population and cout the largest one?

thank you so much!!!!!

actually i don't know how to print out a specific item such as Dallas or USA and also comparing values.....

Last edited by bb0330; 03-06-2008 at 07:17 AM.
 
Old 03-05-2008, 01:24 PM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
strtok

Having programmed in C and then C/C++ nearly exclusively from 1987 through 2003, my first instinct should have been strtok or parsing using one of the std classes. But, no; my first instinct was Linux/Unix command line utilities, cut, sed, or awk.

Just looking at this, strtok or one of the string classes -- I cannot remember which ones if any allow you parsing/search capability -- should allow you to pull out and print what you want nicely. For something this classic, I'd use strtok and parse each line that way.
 
Old 03-05-2008, 08:58 PM   #3
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
What you have posted doesn't look like a csv file. It looks like a space-delimited file, but it's not well-formed, since "New York" should be a single field, not broken by the space.

If, in fact, the fields are separated with commas, then strtok() might be a good solution to identify and store each field.

The balance of your questions are simple programming logic that I assume you have a textbook or class notes to explain.
 
Old 03-06-2008, 07:19 AM   #4
bb0330
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
question updated.

do i need to use the fscanf() function?

please help me, i'm preparing for my exam......

thx a lot!!!!!!!
 
Old 03-06-2008, 07:39 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you're going to use POSIX C, you might as well use regex.h. That way you can do things the way you would with sed.
ta0kira
 
Old 03-06-2008, 09:27 AM   #6
bb0330
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
the following is the code for displaying the whole file in C++......
how can i cout the lines from ID 3793 to 3799?
how can i compare the values of the population and cout the largest one?

Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

void readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
{
        std::string csvLine;
        // read every line from the stream
        while( std::getline(input, csvLine) )
        {
                std::istringstream csvStream(csvLine);
                std::vector<std::string> csvColumn;
                std::string csvElement;
                // read every element from the line that is seperated by commas
                // and put it into the vector or strings
                while( std::getline(csvStream, csvElement, ',') )
                {
                        csvColumn.push_back(csvElement);
                }
                output.push_back(csvColumn);
        }
}

int main()
{
        std::fstream file("country.csv", ios::in);
        if(!file.is_open())
        {
                std::cout << "File not found!\n";
                return 1;
        }
        // typedef to save typing for the following object
        typedef std::vector< std::vector<std::string> > csvVector;
        csvVector csvData;

        readCSV(file, csvData);
        // print out read data to prove reading worked
        for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
        {
                for(std::vector<std::string>::iterator j = i->begin(); j != i->end(); ++j)
                {
                        std::cout << *j << ", ";
                }
                std::cout << "\n";
        }

}
 
Old 03-06-2008, 10:06 AM   #7
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
Your question involves a number of separate issues. It would be best for you to break the problem down into individual component elements that include something like the following:

1. Parsing input. The input appears to be a fairly consistent format consisting of records delimited by lines, each composed of fields delimited by commas. Use standard IO methods to read input by lines, and string methods to disassemble the lines into fields. Some fields may be convertible to numeric data types.

2. Storing the database in memory. The data is already structured, so create data structs that reflect the nature of the data. Probably one or more arrays of structs.

3. Sorting & searching the data structures. More difficult, and highly dependent on how your data structures are created. Use standard C library sort methods, and linear searching for ranges of desired data. You will need to create methods to compare & test pairs of array elements.

4. Reporting. You will need to create methods to print formatted output from arrays of structs or individual structures. This part should be comparatively easy.

Divide and conquer. When you run into problems, it will be easier to ask about specific aspects of your program rather than the general question you've posed here. That makes it easier for others to give good replies.

Once you've accomplished your goal, you will start to understand the value of dtabase management systems such as mySQL, Oracle, & PostgreSQL.


--- rod.

Last edited by theNbomr; 03-06-2008 at 10:09 AM.
 
Old 03-06-2008, 11:39 AM   #8
bb0330
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
Your question involves a number of separate issues. It would be best for you to break the problem down into individual component elements that include something like the following:

1. Parsing input. The input appears to be a fairly consistent format consisting of records delimited by lines, each composed of fields delimited by commas. Use standard IO methods to read input by lines, and string methods to disassemble the lines into fields. Some fields may be convertible to numeric data types.

2. Storing the database in memory. The data is already structured, so create data structs that reflect the nature of the data. Probably one or more arrays of structs.

3. Sorting & searching the data structures. More difficult, and highly dependent on how your data structures are created. Use standard C library sort methods, and linear searching for ranges of desired data. You will need to create methods to compare & test pairs of array elements.

4. Reporting. You will need to create methods to print formatted output from arrays of structs or individual structures. This part should be comparatively easy.

Divide and conquer. When you run into problems, it will be easier to ask about specific aspects of your program rather than the general question you've posed here. That makes it easier for others to give good replies.

Once you've accomplished your goal, you will start to understand the value of dtabase management systems such as mySQL, Oracle, & PostgreSQL.


--- rod.

thank you for your detailed reply :]
but now the most urgent thing for me is i don't know how to do the sorting and searching part, can anyone tell me how to do because my exam is near....

thank you so much!!!!!
 
Old 03-06-2008, 01:07 PM   #9
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
Standard C provides qsort(). Linear searching can be done simply by iterating through array elements.
--- rod.
 
Old 03-06-2008, 09:15 PM   #10
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
Quote:
Originally Posted by JWPurple View Post
If, in fact, the fields are separated with commas...
Which, of course, they are I've got to get my glasses checked.
 
  


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
shell script to read input from csv ip addresses? kr0m3 Programming 3 07-21-2007 08:51 AM
help editing .csv files schneidz Programming 7 06-11-2005 04:09 PM
C++ read csv file row into vector taban1 Programming 3 11-08-2004 02:01 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 01:54 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

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

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