First off , this isn't a homework assignment!!! :p What I want to do is this:
Given a data file(be it text or numbers) saved on the desktop i.e., I want to be able to search that file and pull out only the data I want and print it to the screen. I may want to do other stuff with it but I have no idea what options there are.
Also, would python or c++ be more appropriate. I'm not familiar muc开发者_C百科h with python and it's been years since I've picked up c++ but I've heard that python is more efficient and although this program's efficiency may or may not be a big deal I have heard python is much easier to understand.
Examples,Code, Templates(<-- would be awesome)
Thanks all!
This is a bit difficult to answer without knowing how you want to specify the data you want.
If you can specify the necessary data using regexes, Python will probably be about equally efficient, and a bit quicker to write -- but you may be able to do the job with something like grep even more easily.
If it'll take a lot more processing to figure out what data to display, Python may start to get quite a bit slower -- it can be quite fast as long as the Python part is mostly a fairly "thin" shell and most of the heavy lifting is done by various libraries. It can get quite a bit slower if you're doing serious/significant processing in Python itself.
If you write in in C++, you'll get more or less the opposite situation -- as long as you're reasonably careful, chances are pretty good that performance won't be an issue. The real question will be how much work it takes to produce what you want. Without knowing anything about what data you're looking for, how you want to display it, etc., it's nearly impossible to guess about that though.
edit based on comment: A pattern like Data = ####
sounds like pretty much a classic case for a regular expression, for which grep will work just fine.
This is also something Python can probably do perfectly well, but if you did decide to do your own in C++, it could look something like this:
#include <iostream>
#include <string>
#include <regex>
#include <fstream>
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Usage: searched <filename>\n";
return 1;
}
std::ifstream in(argv[1]);
std::string line;
std::regex pat("Data = [0-9]+");
while (std::getline(line, in))
if (std::regex_search(line, pat))
std::cout << line << "\n";
return 0;
}
This assumes you're looking for the Data = #
pattern happening somewhere in the line. If you want to only consider it a match if that's the whole line, change the regex_search
to regex_match
instead.
The other assumption is that you're using a relatively recent compiler that includes the standard regular expression classes. This is the case with VS 2010 and gcc 4.6 (if I recall correctly) but some older compilers may name it std::tr1::regex
instead, and some that are older still won't have it at all.
C++ will be faster (maybe, if you write it well), but, it will be harder, but easier to start since you know it.
Python will take some time to get used to, and it will probably run a wee bit slower, but, will be easier (once you learn the language).
This is a very easy problem solved numerous times, so, what language you pick really doesn't matter.
If you like a GUI, then look at GUI libraries.
Python will be much better for this task:
for line in file("path/to/file.txt", "rb"):
print line
The equivalent C++ is much more involved.
精彩评论