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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. |
 |
03-05-2008, 12:33 PM
|
#1
|
|
LQ Newbie
Registered: Mar 2008
Posts: 4
|
C++ read & search in .csv files
[ Log in to get rid of this advertisement]
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 08:17 AM..
|
|
|
|
03-05-2008, 02:24 PM
|
#2
|
|
Member
Registered: Feb 2005
Distribution: Centos, Kubuntu, Red Hat
Posts: 394
|
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.
|
|
|
|
03-05-2008, 09:58 PM
|
#3
|
|
Member
Registered: Feb 2008
Posts: 63
|
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.
|
|
|
|
03-06-2008, 08:19 AM
|
#4
|
|
LQ Newbie
Registered: Mar 2008
Posts: 4
|
question updated.
do i need to use the fscanf() function?
please help me, i'm preparing for my exam......
thx a lot!!!!!!!
|
|
|
|
03-06-2008, 08:39 AM
|
#5
|
|
Senior Member
Registered: Sep 2004
Distribution: Slackware 11.0/12.1-current [SELinux]
Posts: 1,606
|
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
|
|
|
|
03-06-2008, 10:27 AM
|
#6
|
|
LQ Newbie
Registered: Mar 2008
Posts: 4
|
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";
}
}
|
|
|
|
03-06-2008, 11:06 AM
|
#7
|
|
Senior Member
Registered: Aug 2005
Location: BC, Canada
Distribution: RH9 FC7,8 SL4,5
Posts: 1,718
|
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 11:09 AM..
|
|
|
|
03-06-2008, 12:39 PM
|
#8
|
|
LQ Newbie
Registered: Mar 2008
Posts: 4
|
Quote:
Originally Posted by theNbomr
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!!!!!
|
|
|
|
03-06-2008, 02:07 PM
|
#9
|
|
Senior Member
Registered: Aug 2005
Location: BC, Canada
Distribution: RH9 FC7,8 SL4,5
Posts: 1,718
|
Standard C provides qsort(). Linear searching can be done simply by iterating through array elements.
--- rod.
|
|
|
|
03-06-2008, 10:15 PM
|
#10
|
|
Member
Registered: Feb 2008
Posts: 63
|
Quote:
Originally Posted by JWPurple
If, in fact, the fields are separated with commas...
|
Which, of course, they are  I've got to get my glasses checked.
|
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:06 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
LQ Podcast
LQ Radio
|
|