Sign in to follow this  
JohnsonGPS

Looking for another way to write c++ code

Recommended Posts

JohnsonGPS    109
I am looking for a different way to write c++ codes, which I think might be more elegant. Here are the codes: ifstream infile; infile.open (sMyFile, ifstream::in); char str[100] = ""; double dMyVar = 0.0; //The focus is on the following 2 lines infile >>str ; //for example, str might be "1.27D-09" dMyVar = FuncApplyStandard (str); //Replace "D" with "e" inside of the func //Convert str to double then return it, infile.close(); Function FuncApplyStandard will replace "D" inside of the string with "e", then return a double value to dMyVar. I would like to know if I can combine the following two lines into 1 line: infile >>str ; //for example, str might be "1.27D-09" dMyVar = FuncApplyStandard (str); Actually I hope I can pass str directly to the function, process it, then pass it to dMyVar. Better as infile >>str >> FuncApplyStandard >> dMyVar; Of course the above line won't work in C++. Could anybody show us how to write a workable line similar to the above? Thank you.

Share this post


Link to post
Share on other sites
visitor    643
Probably more C++-like:


//constructor is capable of opening the line and ifstream is implicitly in reading mode
ifstream infile(sMyFile);

//some error checking here, in case the file couldn't be opened
if (!infile) {...}

//use string type unless you really want to risk buffer overflows
std::string str;


infile >>str;
std::replace(str.begin(), str.end(), 'D', 'e');

double dMyVar = convert_to<double>(str);
//where convert could be implemented by you (perhaps using std::stringstream)
//or you could download boost and use boost::lexical_cast<double>(str);
//this can also fail - who knows what the file really contains - so you should
//somehow deal with it (boost::lexical_cast throws a bad_lexical_cast exception in such case

//no need to explicitly close the file since that is done automatically
//when file stream goes out of scope


Share this post


Link to post
Share on other sites
JohnsonGPS    109
Quote:
Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


Yes it looks definitely better! However I am looking for an even more elegant way.

Share this post


Link to post
Share on other sites
Palidine    1315
elegant != everythingOnOneLine
elegant == easy to read

Whether it's all on one line or not, the compiler is going to make it roughly equivalent. C++ Code is for humans, bytecode is for computers

-me

Share this post


Link to post
Share on other sites
JohnsonGPS    109
Quote:
Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


error C2039: 'replace' : is not a member of 'std'

std::replace doesn't work.

Share this post


Link to post
Share on other sites
ApochPiQ    23005
Quote:
Original post by JohnsonGPS
Quote:
Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


Yes it looks definitely better! However I am looking for an even more elegant way.



What exactly are you defining "elegant" to be? The offered source code is good idiomatic C++ and does the job needed in a fairly robust manner (assuming you bother to do the error checking and other suggested stuff).

Elegance is not the same thing as having to type less code [wink]


Quote:
Original post by JohnsonGPS
Quote:
Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


error C2039: 'replace' : is not a member of 'std'

std::replace doesn't work.


Make sure you #include <algorithm> in your code file.

Share this post


Link to post
Share on other sites
JohnsonGPS    109
Quote:
Original post by ApochPiQ
Quote:
Original post by JohnsonGPS
Quote:
Original post by visitor
Probably more C++-like:

*** Source Snippet Removed ***


Yes it looks definitely better! However I am looking for an even more elegant way.



What exactly are you defining "elegant" to be? The offered source code is good idiomatic C++ and does the job needed in a fairly robust manner (assuming you bother to do the error checking and other suggested stuff).

Elegance is not the same thing as having to type less code [wink]


No, I don't assume that I can define "elegant" of the codes. I just thought, to pass the variables as inputs to the function (the nexst step operation) by >>, is quite straight-forward.

Share this post


Link to post
Share on other sites
visitor    643
If you remove the comments it might get more elegant.

If you want to cram more on one line, you might need a helper function for file input (which here is quite inelegant since it doesn't check whether the read operation succeeded). :


#include <fstream>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>

template <class T>
T read_one(std::istream& is)
{
T s;
is >> s;
return s;
}

int main()
{
try {
std::ifstream fin("a.txt");
double d = boost::lexical_cast<double>(boost::replace_first_copy(read_one<std::string>(fin), "D", "e"));
//...
}
catch (std::exception& e) {
std::cout << e.what();
}
}



Share this post


Link to post
Share on other sites
ApochPiQ    23005
I disagree. The >> operator has a very well defined purpose in the context of reading from stream objects in C++. To add another meaning to the operator as you suggest (i.e. to have it pass parameters to a function) makes your code more confusing, not less.

The goal of a programming language is to communicate to the programmer exactly what the code is doing, and, preferably, why it is doing that. The syntax of reading from a stream, passing the read data into a function, then working with the result (i.e. the syntax given above by visitor) is good because it is not ambiguous.


If you want an extreme example, consider this code. Now, note that if I really bend over backwards, I could actually write working C++ code this way - but it would be much more work and have no benefit, because the purpose of the code is not clear:

a + std::cout - 66[foo] >> "pie";

Share this post


Link to post
Share on other sites
theOcelot    498
I think the definition of elegant that we're looking for is for the code to be both clear and succinct, and robust if it's that sort of region of code (sometimes error checking just needs to be moved somewhere out of the way).

And good heavens, ApochPiQ, please tell me that that isn't actual code! [wow]

Share this post


Link to post
Share on other sites
ApochPiQ    23005
Actually, that is 100% legal C++ and with suitable operator overloads could compile and do something - perhaps even something useful.

This is precisely why doing non-standard, bizarre things with operators is a very, very bad idea.

Share this post


Link to post
Share on other sites
Zahlman    1682
visitor has the right idea here, if you really want to collapse things that way.

To get error checking, set the exception flags on the stream first with the .exceptions() member function. Then you can wrap code in a try/catch block and catch ios::failure if the input is in the wrong format, etc.

Share this post


Link to post
Share on other sites
theOcelot    498
Quote:
Original post by ApochPiQ
Actually, that is 100% legal C++ and with suitable operator overloads could compile and do something - perhaps even something useful.

This is precisely why doing non-standard, bizarre things with operators is a very, very bad idea.


Yeah, I knew it was potentially legal, but I was aghast at the idea that someone would actually produce all the operator overloads to make that compilable.

While I'm here, I couldn't help but notice that Zalhman has become a moderator. How does that happen? I've always wondered...

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:

Yeah, I knew it was potentially legal, but I was aghast at the idea that someone would actually produce all the operator overloads to make that compilable.

I thought not, but then again SiCrane did produce this beauty...

Quote:
Original post by theOcelot
While I'm here, I couldn't help but notice that Zalhman has become a moderator. How does that happen? I've always wondered...

Its a pretty severe operation, or so the saying goes. There is a thread over here where some people are expressing are their surprise or lack thereof. It links to plausible explanation.

I wonder whether the surgery is additive or subtractive... or maybe some odd balance between the two?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this