944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3779
  • C++ RSS
Dec 16th, 2006
0

Adding binary values from a file

Expand Post »
Hello, I'm working o a function that takes a file with binary numbers and returns the sum of those numbers. Here's where I'm now

C++ Syntax (Toggle Plain Text)
  1. int Sum (char *fileName)
  2. {
  3. char * numEnd;
  4. long temp(0);
  5. int result(0);
  6. string line;
  7. ifstream file (fileName);
  8. if (file.is_open())
  9. {
  10. while (! file.eof() )
  11. {
  12. getline (file,line);
  13. //Convert line to an long and sum up with the rest
  14. temp = strtol(line, &numEnd, 2);
  15. result = result + temp;
  16. //Done
  17. }
  18. // close the stream
  19. file.close();
  20. }
  21. return result;
  22. }

and here's an error I'm getting when building it:
error C2664: 'strtol' : cannot convert parameter 1 from 'std::string' to 'const char *'

I thought strtol needed a pointer to the string, but changing line to pointer to line didn't help. Anyone can point me in a right direction?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DynamitMsk is offline Offline
14 posts
since Dec 2006
Dec 16th, 2006
0

Re: Adding binary values from a file

OK, I got it. Any comments on code quality or improvements are still welcome!

C++ Syntax (Toggle Plain Text)
  1. int Sum (char *fileName)
  2. {
  3. char * numEnd;
  4. long temp(0);
  5. int result(0);
  6. string line;
  7. ifstream file (fileName);
  8. if (file.is_open())
  9. {
  10. while (! file.eof() )
  11. {
  12. getline (file,line);
  13. //Convert line to an int and sum up with the rest
  14. char *linePt = &line[0];
  15. temp = strtol(linePt, &numEnd, 2);
  16. result = result + temp;
  17. //Done
  18. }
  19. // close the stream
  20. file.close();
  21. }
  22. return result;
  23. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DynamitMsk is offline Offline
14 posts
since Dec 2006
Dec 17th, 2006
1

Re: Adding binary values from a file

It's because strtol was an old C function written far before C++ strings were invented. Although C++ strings have overloaded functions to make transitions from the older-style char strings to C++ strings and vice-versa, this problem can't be as transparently solved when passing a C++ string to a function that is expecting a char string. To remedy this, C++ strings include a nifty member function called c_str() that can quickly return the char string equivalent. Thus, you could code it like this:
C++ Syntax (Toggle Plain Text)
  1. temp = strtol(line.c_str(), &numEnd, 2);

Better yet, use the C++ streams, the modern equivalent of a lot of the old C string functions, rather than mixing C and C++.

Hope this helps
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: unknown error at run time urgent help needed
Next Thread in C++ Forum Timeline: Bowling game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC