ProgrammingThis 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.
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.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.....
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.
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.
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";
}
}
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.
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....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.