Adding binary values from a file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 14
Reputation: DynamitMsk is an unknown quantity at this point 
Solved Threads: 0
DynamitMsk DynamitMsk is offline Offline
Newbie Poster

Adding binary values from a file

 
0
  #1
Dec 16th, 2006
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

  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 14
Reputation: DynamitMsk is an unknown quantity at this point 
Solved Threads: 0
DynamitMsk DynamitMsk is offline Offline
Newbie Poster

Re: Adding binary values from a file

 
0
  #2
Dec 16th, 2006
OK, I got it. Any comments on code quality or improvements are still welcome!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Adding binary values from a file

 
1
  #3
Dec 17th, 2006
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:
  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
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC